2

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%s\n", s);
    printf("%c", s[2]);

}

int main()
{
    char mystr[5] = "Hello";
    printf("%s\n", mystr);
    printf("%c\n", mystr[4]);

    mystring();
}
2
  • What errors are you getting? Commented Sep 9, 2017 at 14:16
  • 2
    int i = 0; perhaps this scope is wrong Commented Sep 9, 2017 at 14:18

4 Answers 4

1
  1. 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 .

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

  3. Your char mystr[5] = "hello"; has insufficient space, you need to allocate space for the terminating '\0' 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] = '\0';

and,

char mystr[6] = "hello";
Sign up to request clarification or add additional context in comments.

Comments

1

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] = '\0';.

Comments

0

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)

Comments

0

I'm going to comment your code. Hope it helps.

void mystring()
{

    char s[26];//you need one extra space for the null terminating char, so it should be "char s[27]"
    for(char ch = 'A'; ch < 'Z'; ch++)//if you want to print 'Z' as well, this should be "ch <= 'Z'", otherwise it stops at 'Y', which is the last char less than 'Z'
    {
        int i = 0;//you should declare this i before the loop, along with your char array. The way you're doing it right now, on every loop iteration you redeclare i and give it the value 0, meaning you're just filling the first position of your char array over and over
        s[i] = ch;
        printf("%c", s[i]);
        i++;
    }
    //s[26] = '\0'; //you should add this line <--remember, strings in C are null-terminated
    printf("\n%s\n", s);
    printf("%c", s[2]);

}

int main()
{
    char mystr[5] = "Hello";//strings in C are null terminated so this should have an extra space "char mystr[6]"
    printf("%s\n", mystr);
    printf("%c\n", mystr[4]);

    mystring();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.