//INPUT : hello the world
// OUTPUT : world the hello
#include<stdio.h>
void reverse(char *str,int i,int j){
char temp;
while(i<j){
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
}
void reverseWords(char *str){
int len = strlen(str);
int start,end;
int i=0;
reverse(str,0,len-1);
while(str[i] != '\0'){
if(str[i] == ' '){
i++;
continue;
} else{
start = i;
while(str[i] != '\0' && str[i] != ' '){
i++;
}
end = i-1;
reverse(str,start,end);
}
}
}
int main(){
char str[] = "hello the world";
reverseWords(str);
printf("%s",str);
return 0;
}
No comments:
Post a Comment