Thread: why can't a char array use and int index?

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    3

    why can't a char array use and int index?

    Hi. I am confused why I cannot do this to display each element of my char string? What is the proper way? Thank you.

    Code:
    char str[SIZE];
    int i;
    
       for(i=0; i<SIZE; i++)
       {
       printf("%s", str[i]);
       }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You can print each character individually, but not with the %s specifier which prints a string and expects a pointer to the string. Use %c for characters.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    3
    Thanks for the quick reply. But what if each element of the char array is a word. For ex.
    Code:
    char str[] = "my name is ...";
    and I am trying to extract each individual word?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The elements of a char array are single characters, not words.

    The fact that you (or I) deem that groups of characters separated by whitespace are words, doesn't make any difference to printf(). Using %s will cause printf() to write out the whole string (all the characters, one after the other, in one hit - no loop required by you). Using %c, in a loop, simply means you make the outputting of each character more explicit.

    No need to "extract" the words if all you're going to do is print them in the same order, with whitespace in between.

    However, this code will print each word in a string on a separate line.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    void PrintWordsOnLine(const char s[])
    {
        int index = 0;
        while (s[index] != 0)
        {
             if (isspace(s[index]))     /*  isspace checks for whitespace */
                printf("\n");
             else
                printf("%c", s[index]);
             ++index;
        }
        printf("\n");   /*  outputs a new line after the last word */
    }
    
    int main()
    {
         PrintWordsOnLine("The brown cow jumped over the moon");  
         return 0;
    }
    This does not exactly "extract" the separate words. It simply prints out all characters one at a time, but substitutes a carriage return for any whitespace character. So if there are multiple consecutive spaces, you'll see blank lines. At no point, however, is there something that represents a separate word. But this should be enough to get you started with extracting words, depending on what you actually mean by "extract".
    Last edited by grumpy; 03-02-2013 at 08:57 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    3
    Quote Originally Posted by grumpy View Post
    The elements of a char array are single characters, not words.

    The fact that you (or I) deem that groups of characters separated by whitespace are words, doesn't make any difference to printf(). Using %s will cause printf() to write out the whole string (all the characters, one after the other, in one hit - no loop required by you). Using %c, in a loop, simply means you make the outputting of each character more explicit.

    No need to "extract" the words if all you're going to do is print them in the same order, with whitespace in between.

    However, this code will print each word in a string on a separate line.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    void PrintWordsOnLine(const char s[])
    {
        int index = 0;
        while (s[index] != 0)
        {
             if (isspace(s[index]))     /*  isspace checks for whitespace */
                printf("\n");
             else
                printf("%c", s[index]);
             ++index;
        }
        printf("\n");   /*  outputs a new line after the last word */
    }
    
    int main()
    {
         PrintWordsOnLine("The brown cow jumped over the moon");  
         return 0;
    }
    This does not exactly "extract" the separate words. It simply prints out all characters one at a time, but substitutes a carriage return for any whitespace character. So if there are multiple consecutive spaces, you'll see blank lines. At no point, however, is there something that represents a separate word. But this should be enough to get you started with extracting words, depending on what you actually mean by "extract".
    Thank you very much. This is very helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-04-2012, 09:03 PM
  2. Replies: 1
    Last Post: 08-12-2011, 03:07 AM
  3. Replies: 8
    Last Post: 12-08-2009, 02:47 AM
  4. index array
    By kurz7 in forum C Programming
    Replies: 9
    Last Post: 04-24-2003, 08:57 AM
  5. About Array Index
    By Kokila in forum C Programming
    Replies: 3
    Last Post: 11-08-2001, 12:29 PM