Skip to content

Instantly share code, notes, and snippets.

@gallirohik
Created February 21, 2018 19:04
Show Gist options
  • Save gallirohik/ccaf19935c947d17075fd4d01a864df1 to your computer and use it in GitHub Desktop.
Save gallirohik/ccaf19935c947d17075fd4d01a864df1 to your computer and use it in GitHub Desktop.
get reverse of words
#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