Explore BrainMass

Explore BrainMass

    A simple C program to separate words by spaces

    Not what you're looking for? Search our solutions OR ask your own Custom question.

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    Write a C program that accepts a string of characters from a terminal and displays the string one word per line. Make your array 80 characters and assume the entered text will be less than 80 characters. A complete C program is included as well as a description of how the program works.

    © BrainMass Inc. brainmass.com September 27, 2022, 5:05 pm ad1c9bdddf
    https://brainmass.com/computer-science/c/simple-c-program-separate-words-spaces-434122

    SOLUTION This solution is FREE courtesy of BrainMass!

    There a many ways to do this but one of the most straight-forward approaches is to process the string one character at a time. When the character is not a space character, just write it to the screen. When the character is a space character, write a newline. The main loop is this:

    len = strlen(s);
    for (i=0;i<len;i++) {
    if (s[i] == ' ') {
    printf("n");
    } else {
    printf("%c",s[i]);
    }
    }

    The entire program is attached.

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    © BrainMass Inc. brainmass.com September 27, 2022, 5:05 pm ad1c9bdddf>
    https://brainmass.com/computer-science/c/simple-c-program-separate-words-spaces-434122

    Attachments

    ADVERTISEMENT