1. Limited time only! Sign up for a free 30min personal 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!

Homework Help: 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:

    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: ");
        return 0;
  2. jcsd
  3. Nov 18, 2013 #2


    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted