reverse every word

Reversing Every Word in C Language

To reverse every word in a given string using the C language, you can follow these steps:

  1. Tokenize the String: Split the input string into individual words.
  2. Reverse Each Word: Reverse each word in the tokenized string.
  3. Reconstruct the String: Reconstruct the reversed words into a single string.

Here's a sample code snippet to achieve this:

#include <stdio.h>
#include <string.h>

void reverseWords(char *str) {
    char *word = strtok(str, " ");
    while (word != NULL) {
        int len = strlen(word);
        for (int i = len - 1; i >= 0; i--) {
            printf("%c", word[i]);
        }
        printf(" ");
        word = strtok(NULL, " ");
    }
}

int main() {
    char str[] = "Hello World";
    reverseWords(str);
    return 0;
}

In this code: - We use strtok to tokenize the input string based on spaces. - Then, we reverse each word and print the reversed words with spaces in between.

This code will reverse every word in the input string "Hello World" and print the result as "olleH dlroW".