#include <stdio.h>
#include <string.h>
#include <cs50.h>

int main ()
    string s = GetString();
    int n=strlen (s);
    string c [n];

    for (int i=0; i< n; i++)
        printf ("%c\n", c[i]);

1 Answer 1


Because you are creating c as an array of strings. I think you meant to make c an array of n chars. Remember that a string is an array of chars in which the last char is a null terminator \0.

I suggest you change this line:

string c [n];


char c[n];

Since you are printing c character by character you don't need to add the null terminator at the end. But, if you want to treat c as a string further on, then you could instead write the same line as:

char c[n+1]; // +1 to fit the null terminator

and add a null terminator right after (outside of) the loop for example:

c[n] = '\0';

You must log in to answer this question.

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