Created
December 1, 2016 07:01
-
-
Save Highstaker/392bd1e9c293f806006de6c5befaadea to your computer and use it in GitHub Desktop.
reverses a string of space-separated words terminated by newline
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
// reverses a string of space-separated words terminated by newline | |
#include <stdio.h> | |
#include <stdlib.h> | |
void swap(char* arr, int i, int j){ | |
char t = arr[j]; | |
arr[j] = arr[i]; | |
arr[i] = t; | |
} | |
void string_reverse(char* arr, int s, int e){ | |
int l = s; | |
int r = e; | |
while(l<r){ | |
swap(arr,l,r); | |
l++;r--; | |
} | |
} | |
void word_reverser(char* arr){ | |
int len = 0; | |
for(;arr[len]!='\n';len++); | |
printf("%s:%d\n", "Input length",len); | |
string_reverse(arr, 0, len-1); | |
int i=0, j=0; | |
for(i=0;i<len;){ | |
for(j=i;arr[j] != ' ' && j<len; j++)printf("%d\n", j); | |
string_reverse(arr, i, j-1); | |
i=j+1; | |
} | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
printf("%s:", "Type in text"); | |
char inp[10000]; | |
fgets(inp, 10000, stdin); | |
word_reverser(inp); | |
printf("%s\n", inp); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment