Introduction:

This C program reverses the words in a given string while maintaining the integrity of the characters within each word. The program reads a string from the user, processes it to rearrange the words in reverse order, and then displays the modified string. This functionality can be useful in various applications such as text formatting, data presentation, and algorithm demonstrations.

#include <stdio.h>  
#include <string.h>  
int main()  
{  
    char str[40]; // declare the size of character string  
    printf (" \n Enter a string to be reversed: ");  
    scanf ("%s", str);  
      
    // use strrev() function to reverse a string  
    printf (" \n After the reverse of a string: %s ", strrev(str));  
    return 0;  
}  

Include Header Files:

  • #include <stdio.h>: For standard input and output functions like printf and fgets.
  • #include <string.h>: For string manipulation functions like strlen.

Function to Reverse a Portion of the String:

  • void reverse(char *str, int start, int end): This function reverses a portion of the string from the start index to the end index.
  • A temporary variable temp is used to swap characters.
  • The function uses a while loop to swap characters until the start index is greater than or equal to the end index.

Function to Reverse Words in the String:

  • void reverseWords(char *str): This function reverses the order of words in the input string.
  • int length = strlen(str);: Calculates the length of the string.
  • int start = 0;: Initializes the starting index for word reversal.

Reverse the Entire String:

  • The first call to reverse(str, 0, length - 1); reverses the entire string. This positions the words in reverse order.

Reverse Each Word:

  • The for loop iterates through each character in the string. It checks for spaces or the null terminator (\0) to identify word boundaries.
  • When a space or the end of the string is found:
    • reverse(str, start, i - 1);: Reverses the characters of the word identified by start to i - 1.
    • start = i + 1;: Moves the starting index to the beginning of the next word.

Main Function:

  • char str[100];: Declares an array to hold the user-input string.
  • printf("Enter a string: ");: Prompts the user for input.
  • fgets(str, sizeof(str), stdin);: Reads a line of text, including spaces, into str.
  • str[strcspn(str, "\n")] = 0;: Removes the newline character added by fgets.
  • reverseWords(str);: Calls the function to reverse the words in the string.
  • printf("String after reversing words: %s\n", str);: Outputs the modified string.

Return Statement:

  • return 0;: Indicates successful completion of the program

Input/Output Block:

Input:

  • The user is prompted to enter a string containing words.

Example Input:

Enter a string to be reversed: Hello World

Output:

  • The program outputs the string with the words reversed.

Example Output:

After the reverse of a string: World Hello

Conclusion:

The reverse words program effectively rearranges the words in a string, showcasing fundamental concepts in string manipulation and memory handling in C. By utilizing a two-step approach—reversing individual words and then the entire string—the program achieves the desired outcome efficiently. This capability can be applied in various contexts, such as formatting text for display or processing user inputs in applications. The program serves as a practical example of algorithm design and implementation in C, making it a useful addition to any programmer’s repertoire.

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights