C Program to Reverse Letter in Each Word of the Entered String
Write a C Program to Reverse Letter in Each Word of the Entered String
In this program we are going to accept a string . This program will check for word inside string and if word founds then program will reverse that word. Similarly it will reverse out all all the words.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include<stdio.h> #include<conio.h> #include<string.h> void main() { char msg[] = "Welcome to Programming World"; char str[10]; int i = 0, j = 0; clrscr(); while (msg[i] != '\0') { if (msg[i] != ' ') { str[j] = msg[i]; j++; } else { str[j] = '\0'; printf("%s", strrev(str)); printf(" "); j = 0; } i++; } str[j] = '\0'; printf("%s", strrev(str)); getch(); } |
Output :
1 |
emocleW ot gnimmargorP dlroW |
Explanation of C Program :
Step 1 : Space Character Encountered
1 2 3 4 5 6 7 8 9 |
if (msg[i] != ' ') { str[j] = msg[i]; j++; } else { str[j] = '\0'; printf("%s", strrev(str)); printf(" "); j = 0; } |
Store Character into Another String Variable. As soon as Space character encounters then reverse the string and print the String.