Created
August 19, 2017 02:46
-
-
Save ibreakthecloud/a30b6670bd0660add572e31e6afb9028 to your computer and use it in GitHub Desktop.
Reverse words from the user inputted string, e.g. Input: Hello world Output: world Hello
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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
char* strev(char str[]) | |
{ | |
int i,j; | |
char temp[100]; | |
i = 0; | |
j = strlen(str) - 1; | |
while (i < j) { | |
temp[1] = str[i]; | |
str[i] = str[j]; | |
str[j] = temp[1]; | |
i++; | |
j--; | |
} | |
return str; | |
} | |
int main() | |
{ | |
char a[1000],temp[1000],*rev_a,reverse[1000]="",buffer[500]; | |
int j,len,i,testcase,tc=1,ch; | |
//fflush(stdin); | |
scanf("%d",&testcase); | |
while ((ch = getchar()) != '\n' && ch != EOF); | |
//fflush(stdin); | |
while(tc <= testcase) | |
{ | |
sprintf(buffer, "Case #%d: ",tc); | |
strcat(reverse,buffer); | |
fgets(a,100,stdin); | |
rev_a = strev(a); | |
len = strlen(a); | |
j=0; //result = NULL; | |
for(i=0;i<len;i++) | |
{ | |
for(j=0; i<len && rev_a[i]!=' ' && rev_a[i]!='\n'; ++i,++j) | |
{ | |
temp[j] = rev_a[i]; | |
} | |
temp[j] = '\0'; | |
strcat(reverse, strev(temp)); | |
if(i>1 && i<len) | |
strcat(reverse, " "); | |
} | |
strcat(reverse,"\n"); | |
tc++; | |
} | |
printf("%s",reverse); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment