1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

String question

  1. Nov 18, 2013 #1
    The lab requires me to displace that:
    Enter a string:This is a string

    One word per line is:
    This
    is
    a
    string

    How can I replace the ' 'with Enter.

    Code (Text):
    #include <stdio.h>
    #define STRSIZE 81

    void dispStr(char message[STRSIZE])
    {
        int i;
       
        printf("\nOne word per line is :\n");
         
     
       while (message[i]!=' ')
       {
           
       }
       
    }

    int main()
    {
        char message[STRSIZE];
       
        printf("Enter a string: ");
        gets(message);
        dispStr(message);
       
        return 0;
    }
    Thanks.
     
  2. jcsd
  3. Nov 18, 2013 #2

    Mark44

    Staff: Mentor

    There are a couple problems with the code in your dispStr function.
    1. Your loop variable is declared, but no initialized. This will probably cause your loop to try to access memory that doesn't belong to the string. Your loop control variable (i) should be initialized to 0.
    2. You don't show any code that increments the loop variable.

    To answer your question, when the loop encounters a space character, it should print a newline character ('\n'). You also need logic in our loop so that it prints all of the characters that precede the space before it prints the newline character.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: String question
  1. Strings in C (Replies: 6)

  2. String in java (Replies: 7)

Loading...