Created
July 31, 2017 08:31
-
-
Save 0001vrn/ef55b4a0ae31c57a9fcf725ad1ce6d3d to your computer and use it in GitHub Desktop.
Reverse words in a given string Raw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a String of length N reverse the words in it. Words are separated by dots. | |
By : Varun Thakur | |
Date : 31/07/2017 | |
*/ | |
#include <stdio.h> | |
void reverse(char *begin, char *end) | |
{ | |
char temp; | |
while (begin < end) | |
{ | |
temp = *begin; | |
*begin++ = *end; | |
*end-- = temp; | |
} | |
} | |
void reverseWords(char *s) | |
{ | |
char *word_begin = s; | |
char *temp = s; | |
while(*temp) | |
{ | |
temp++; | |
if(*temp == '\0') | |
{ | |
reverse(word_begin,temp-1); | |
} | |
else if(*temp == '.') | |
{ | |
reverse(word_begin, temp-1); | |
word_begin = temp+1; | |
} | |
} | |
reverse(s,temp-1); | |
} | |
int main() { | |
//code | |
int t;scanf("%d",&t); | |
while(t--){ | |
char s[20005]; | |
scanf("%s",&s); | |
reverseWords(s); | |
printf("%s\n",s); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment