Created
February 21, 2018 19:04
-
-
Save gallirohik/ccaf19935c947d17075fd4d01a864df1 to your computer and use it in GitHub Desktop.
get reverse of words
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> | |
int length(char *str) | |
{ | |
int i=0; | |
while(*str!='\0') | |
{ | |
i++; | |
str++; | |
} | |
return i; | |
} | |
void reverse(char *str,int start,int end) | |
{ | |
char temp; | |
int i,j; | |
for(i=start,j=end;i<j;i++,j--) | |
{ | |
temp=str[i]; | |
str[i]=str[j]; | |
str[j]=temp; | |
} | |
} | |
void reverse_words(char *str) | |
{ | |
int start=0,end,i=0,flag=1; | |
while(str[i]!='\0') | |
{ | |
if(flag) | |
{ | |
if(str[start]!=' ') | |
start=i; | |
flag=0; | |
} | |
if(str[i]==' ') | |
{ | |
end=i-1; | |
reverse(str,start,end); | |
flag=1; | |
} | |
i++; | |
} | |
end=i-1; | |
reverse(str,start,end); | |
} | |
int main() | |
{ | |
//code | |
char str[50]; | |
int i=0,len; | |
scanf("%[^\n]%*c",str); | |
reverse_words(str); | |
printf("%s",str); | |
reverse(str,0,length(str)-1); | |
printf("\n%s",str); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment