Thread: Printing out char elements in an array from user input

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    23

    Printing out char elements in an array from user input

    First post on this website, I'm a newbie to C Programming. I've got my head around data types.

    I want to be able to ask the user to input a string of text and store it in an array. I have created the following test program to see if my code is able to display the 3rd char element of what the user had input.
    Code:
    #include <stdio.h>
    
    int main()
    {
    char string[10];
    
    printf("enter some text");
    scanf ("%s", string);
    
    printf("%s", string[3]);
    
    return 0;
    }
    What this program does instead is crash on me, any idea why?
    Also since I've declared the number 10 in the string, does this mean that my program will indeed accept only 9 char values? (*-1)

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You're close, but you need to use a different format string:
    Code:
    printf("%c", string[3]); /* third character isn't 3 */
    > Also since I've declared the number 10 in the string, does this mean that my program will indeed accept only 9 char values?
    No, that would be convoluted if entering 10 resulted in 9. The reason why you're probably confused is because it indexes from zero: 0-9 instead of 1-10, but they can both hold the same amount of characters. Knowing that, change your printf() accordingly.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Because what "char string[10]; " does is create an array of ten character wide memory locations (usually 1 byte).
    You can't cram a string in that location.
    Instead you need to create a 2D or string array as below:

    Code:
    #include <stdio.h>
    
    int main (void) {
    
          char string[5][10];
            
          printf("enter some text\n");
          scanf ("%s", string[3]);
            
          printf("%s\n", string[3]);
          printf("%s\n", string[4]);
            
            
       return 0;
    }

    The problem with scanf as this program will show, is it doesn't know when to stop accepting characters from you entered string.
    If you enter hhhhhkkkkklllll that is 15 characters, it will keep filling up into the next slot 'string[4]'.
    Later you will learn fgets() which addresses that.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > Later you will learn fgets() which addresses that.
    You could just use scanf length specifiers:
    Code:
    scanf("%10s", string);
    But you're right, fgets is something you should learn, especially for raw string input.

    > You can't cram a string in that location.
    Yes, you can. And a 2D array is not going to solve the problem if you couldn't.

    > it will keep filling up into the next slot 'string[4]'.
    Right, but that's not what you want. It's safe, but you can't access the memory again unless you know what got overwritten and where to find it.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Sheesh, people. Please give correct advice or none at all.
    chart string[10] will indeed allow only 9 characters of input the way it is implied in the example. There is s trailing null byte in the last position - as there is with any string. So you are only allowed array-length-1 entered characters. Total is 10.

    There was no reason to bring up 2D when the user only wanted to display the 3rd char.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by memcpy View Post
    > Later you will learn fgets() which addresses that.
    You could just use scanf length specifiers:
    Code:
    scanf("%10s", string);
    Actually that should be:

    Code:
    scanf("%9s", string);
    if the string is declared as: string[10];

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by memcpy View Post
    >

    > You can't cram a string in that location.
    Yes, you can. And a 2D array is not going to solve the problem if you couldn't.
    What I meant was you can not cram a string into a 1 byte char memory location. Not the whole array.
    If he want's 10 strings 20 characters wide, then string[10][21] could handle that.
    My fault for not being more specific.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by nonoob View Post
    Sheesh, people. Please give correct advice or none at all.
    chart string[10] will indeed allow only 9 characters of input the way it is implied in the example. There is s trailing null byte in the last position - as there is with any string. So you are only allowed array-length-1 entered characters. Total is 10.

    There was no reason to bring up 2D when the user only wanted to display the 3rd char.
    Oops. you're right. I read it wrong.
    In that case, memcpy's first answer is correct. Change printf("%s.... to printf("%c...

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    Thanks all, the data type change did sort the problem out after all.. I'm now trying to print out the occurrence of a char typed in to the string..
    (I've spent some time trying to accomplish this task but hit a problem..)

    this is a part of my code (string is similar to the first post, it is the users previously entered input and stored into the array "string"):

    Code:
    for (scounter=0; scounter<strlen(string); scounter++)
    {
        if (string[scounter] = 'a')
        {
            chara++;
        }
        if (string[scounter] = 'b')
        {
            charb++;
        }
    printf ("%d", chara); 
    return 0;
    I just want to test how many times the character 'a' was entered but instead all i get back is the length of the string entered and not how many times 'a' was entered?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    = is an assignment, not a comparson for equality.

    == is a comparison for equality.

  11. #11
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    Quote Originally Posted by Adak View Post
    = is an assignment, not a comparson for equality.

    == is a comparison for equality.
    Thanks! That fixed it, another thing I've learnt now.

    I've gone and implemented the code to update all counters of the alphabet, however all I've done now is print out the number assigned to the counters for each letter in the alphabet. It kind of gives it a messy look when its outputted if vast majority of the letters are not entered, so my question is, how can I go about outputting the char's given by the user instead so I can only output the occurrence of those letters instead of the entire alphabet?

    I was thinking of an else if statement, but if I use the OR operator for each char then it'll end up printing it all out anyway as it only needs to satisfy one condition...

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Use an array of letter counters - each element representing the count of the corresponding letter of the alphabet. When you display the results you skip over the zero values.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing out previously entered user input!
    By SimarGill in forum C Programming
    Replies: 9
    Last Post: 12-04-2011, 10:19 PM
  2. Structure, user input and printing out results
    By alvarito in forum C Programming
    Replies: 3
    Last Post: 10-04-2011, 11:04 AM
  3. Printing asterisks using user input
    By celticpride in forum C Programming
    Replies: 4
    Last Post: 02-10-2011, 09:43 PM
  4. Printing out Array Elements
    By BB89 in forum C++ Programming
    Replies: 6
    Last Post: 03-24-2010, 02:00 PM
  5. letting user input number of elements in array
    By iamthejake2000 in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2005, 12:35 PM