C program to reverse words in a string

5 (100%) 4 votes

C program to reverse words in a string

This profram will invert each word occurring in the input string.

Algorithm is very simple just scan the string and keep on storing characters until a space comes. If a space is found then we have found one word so we append null terminator and then reverse the word and then copy the characters of original string with the string obtained on reversing.

Then repeat the previous step until the string ends.

 

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

void reverse_string(char*);
void reverse_words(char*);

int main() {
char a[100];

gets(a);

reverse_words(a);

printf(“%s\n”, a);

return 0;
}

void reverse_words(char *s) {
char b[100], *t, *z;
int c = 0;

t = s;

while(*t) { //processing complete string
while(*t != ‘ ‘ && *t != ‘\0’) { //extracting word from string
b[c] = *t;
t++;
c++;
}
b[c] = ‘\0’;
c = 0;

reverse_string(b); // reverse the extracted word

z = b;

while (*z) { //copying the reversed word into original string
*s = *z;
z++;
s++;
}

while (*s == ‘ ‘) { // skipping space(s)
s++;
}
/*
* You may use if statement in place of while loop if
* you are assuming only one space between words. If condition is
* used because null terminator can also occur after a word, in
* that case we don’t want to increment pointer.
* if (*s == ‘ ‘) {
* s++;
* }
*/
t = s; // pointing to next word
}
}

/*
* Function to reverse a word.
*/

void reverse_string(char *t) {
int l, c;
char *e, s;

l = strlen(t);
e = t + l – 1;

for (c = 0; c < l/2; c++) {
s = *t;
*t = *e;
*e = s;
t++;
e–;
}
}

 

 

For any query or issue, feel free to discuss on http://discuss.eduguru.in
%d bloggers like this: