printing strings populated with a loop in C?

I'm new to C and trying to learn about printing strings and single elements of arrays.

In the main function I have created a char array of 5 characters and am able to print the string. I am also able to print 1 element of the array. This all works fine.

I have then created a function which has a for loop to populate an array with 26 available slots and want to do the same thing, print the whole string and then just print a single element of it.

When my program executes it should print: Hello o ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ (This should print twice, once for the for loop and then once when I try to print the string) C

However when I try to print the characters this time, I am running into errors and the only difference I can see is the way I have initialised the array. I don't understand the difference and why it is breaking. Is it something to do with the scope of the for loop? I also tried making the char array static but that didn't work either. Can anyone help with this?

Here's my code

I'm new to C and trying to learn about printing strings and single elements of arrays.

In the main function I have created a char array of 5 characters and am able to print the string. I am also able to print 1 element of the array. This all works fine.

I have then created a function which has a for loop to populate an array with 26 available slots and want to do the same thing, print the whole string and then just print a single element of it.

When my program executes it should print: Hello o ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ (This should print twice, once for the for loop and then once when I try to print the string) C

However when I try to print the characters this time, I am running into errors and the only difference I can see is the way I have initialised the array. I don't understand the difference and why it is breaking. Is it something to do with the scope of the for loop? I also tried making the char array static but that didn't work either. Can anyone help with this?

void mystring()
{

    char s[26];
    for(char ch = 'A'; ch < 'Z'; ch++)
    {
        int i = 0;
        s[i] = ch;
        printf("%c", s[i]);
        i++;
    }

    printf("n%sn", s);
    printf("%c", s[2]);

}

int main()
{
    char mystr[5] = "Hello";
    printf("%sn", mystr);
    printf("%cn", mystr[4]);

    mystring();
}

  • There is no null terminator for both your "strings", I write "strings" because they are in effect NOT strings according to the definition of string in c.

  • Your i variable is always 0 , just use a simple formula ch - 'A' will give the correct index.

  • Your char mystr[5] = "hello"; has insufficient space, you need to allocate space for the terminating '' there too.

  • To fix your code, you can do this

    char s[27];
    for (int ch = 'A'; ch <= 'Z'; ++ch) {
        s[ch - 'A'] = ch;
    }
    s[26] = '';
    

    and,

    char mystr[6] = "hello";
    

    First you should declare the i value outside from the for loop otherwise the i++ command has no effect since the i will always be 0 .

    In C strings are arrays with char elements so at the end of these containers you should always put the null character. So in order to be specific you have to increment by 1 the size of your array (27 not 26) and in the final position of your string, when you are done creating it, s[26] = ''; .


    Currently, the major error I see is

    for(char ch = 'A'; ch < 'Z'; ch++)
        {
            int i = 0;
            s[i] = ch;
            printf("%c", s[i]);
            i++;
        }
    

    You want to move the the declaration and initialization statement ( int i = 0 ) outside the loop. Right now, it is resetting i to 0 each time (and actually, probably giving you the error since it tries to declare a variable with the same name)

    链接地址: http://www.djcxy.com/p/72160.html

    上一篇: puts()不在main()中输出字符串,但在函数中打印出来很好

    下一篇: 打印字符串填充循环在C?