Created
February 11, 2017 16:25
-
-
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.
This file contains hidden or 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> | |
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