C Program to Reverse every Word of given String

«
»

This is a C Program to reverse every word of given string.

Problem Description

This program takes a string and reverses every word of the string.

Problem Solution

1. Take a string as input.
2. Store each word of the input string in the separate rows of the 2-D array.
3. Reverse each word of the string.

advertisement
Program/Source Code

Here is source code of the C Program to reverse every word of given string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1.  
  2. /* 
  3.  *  C Program to Reverse every Word of given String
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. void main()
  9. {
  10.     int i, j = 0, k = 0, x, len;
  11.     char str[100], str1[10][20], temp;
  12.  
  13.     printf("enter the string :");
  14.     scanf("%[^\n]s", str);
  15.  
  16. /* reads into 2d character array */
  17.     for (i = 0;str[i] != '\0'; i++)
  18.     {
  19.         if (str[i] == ' ')
  20.         {
  21.             str1[k][j]='\0';
  22.             k++;
  23.             j=0;
  24.         }
  25.         else
  26.         {
  27.             str1[k][j]=str[i];
  28.             j++;
  29.         }
  30.     }
  31.     str1[k][j] = '\0';
  32.  
  33. /* reverses each word of a given string */
  34.     for (i = 0;i <= k;i++)
  35.     {
  36.         len = strlen(str1[i]);
  37.         for (j = 0, x = len - 1;j < x;j++,x--)
  38.         {
  39.             temp = str1[i][j];
  40.             str1[i][j] = str1[i][x];
  41.             str1[i][x] = temp;
  42.         }
  43.     }
  44.     for (i = 0;i <= k;i++)
  45.     {
  46.         printf("%s ", str1[i]);
  47.     }
  48. }
Program Explanation

1. Take a string as input and store it in the array str[].
2. Using for loop store each word of the input string into the 2-D array str1[][].
3. In the 2-D array str1[][] reverse each word of the string at each row of the array.
4. Print the 2-D array as output.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Runtime Test Cases
enter the string :C Programming Class
C gnimmargorP ssalC

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

advertisement
If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.