Skip to content

Instantly share code, notes, and snippets.

@SunilDSK
Created February 11, 2017 16:25
Show Gist options
  • Save SunilDSK/73213b2ec1636be3a4f36cb39b41b440 to your computer and use it in GitHub Desktop.
Save SunilDSK/73213b2ec1636be3a4f36cb39b41b440 to your computer and use it in GitHub Desktop.
C program to reverse the words in a string (Sentence) using pointers only.
#include <stdio.h>
#include <stdlib.h>
void swap(char *c1, char *c2){
char temp;
temp = *c1;
*c1 = *c2;
*c2 = temp;
}
int main(){
char string[] = "This is";
char *start = string;
char *end = start;
char *end2 = end;
while(*end != '\0'){
while(*end !=' ' && *end != '\0'){end++;end2++;}
end--;
while(start<end){
swap(start,end);
start++;end--;
}
end2++;
start = end = end2;
}
printf("%s",string);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment