Complementary task for topic: 7

M Nemeth · 2023-08-29 15:21:04.623218'

Strings: Word Reversal in a Sentence

Strings: Word Reversal in a Sentence

Create a C program that reads a sentence from the user and reverses each word in the sentence. The program should display the modified sentence as the output.
Library functions can be used!

Hint: Use character arrays (char arrays) to store the sentence and individual words.
Prompt the user to enter a sentence.
Tokenize the sentence into individual words (words are separated by spaces).
Reverse each word in the sentence.
Display the modified sentence as the output.
strtok(char str[], char delim[]) if a function that gives back a string that ends with the next delim[] (or EoS) (here " " string that contains a space), gives back NULL, if there is no more delim[] string found, strtok(NULL," "); will give you the next token

Solution
#include 
#include 

// Function to reverse a word
void reverseWord(char word[]) {
    int length = strlen(word);
    int start = 0;
    int end = length - 1;
    
    while (start < end) {
        char temp = word[start];
        word[start] = word[end];
        word[end] = temp;
        
        start++;
        end--;
    }
}

int main() {
    char sentence[200];
    char modifiedSentence[200] = "";
    char word[50];

    printf("Enter a sentence: ");
    scanf("%199[^\n]", sentence);

    // Tokenize the sentence into individual words
    char* token = strtok(sentence, " ");
    while (token != NULL) {
        // Reverse the current word and append it to the modified sentence
        strcpy(word, token);
        reverseWord(word);
        strcat(modifiedSentence, word);
        strcat(modifiedSentence, " ");

        token = strtok(NULL, " ");
    }

    printf("Modified sentence: %s\n", modifiedSentence);

    return 0;
}



Explanation
    The program defines a character array (char array) sentence to store the input sentence, a character array modifiedSentence to store the modified sentence, and a character array word to store individual words.

    The user is prompted to enter a sentence using printf() and scanf(). The %199[^\n] format specifier is used to read the entire line until a newline character (\n) is encountered, allowing the program to read a sentence with spaces.

    The program uses strtok() function from the string.h library to tokenize the sentence into individual words. The delimiter used is a space " ", which separates words in the sentence.

    The program defines a function reverseWord() to reverse a given word. The function takes a character array word as input and reverses the characters in-place.

    Inside the loop, the program copies the current word into the word array and calls the reverseWord() function to reverse the word.

    The reversed word is then appended to the modifiedSentence along with a space, forming the modified sentence.

    After processing all the words in the sentence, the program displays the modified sentence using printf().
< < previous    next > >