0

My code just stops after I get the string from the user, and I'm not sure why it isn't continuing. Any pointers?

int main (void) {

    printf("What is your full name?\n");
    string s = get_string();


    int i;
    int n;
    for ( i = 0, n = strlen(s); i < n; i++) {

        if (s[i] == '\0') {

            printf("%c%c\n",s[0], s[i+1]);
        }

    }

}

1 Answer 1

0

If s is "bob" it occupies 4 bytes (including the terminating null byte), with indexes 0 through 3. strlen(s) is 3. The null terminator ('\0') is in s[3]. The loop ends when i equals 3. Therefore, the if condition is never true. The program does not stop; it iterates through the loop, but it produces no output.

1
  • Thank you so much!
    – Jrazi
    Apr 12, 2017 at 1:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .