Subject:
Given an input string, reverse the string word by word.
For example,
Given s = “the sky is blue”,
return “blue is sky the”.
Code:
//
// main.c
// ReverseWord
//
// Created by sj wang on 01/02/2017.
// Copyright © 2017 sj wang. All rights reserved.
//
#include <stdio.h>
void printWord(char[], int);
int main(int argc, const char * argv[]) {
// insert code here...
char ch[1000];
gets(ch);
int i,head;
//Locate the end index of the last character in the sentence
while(ch[i]!='\0'){
i++;
}
//When meeting blank, print the word after blank
while(i>=-1){
if(ch[i]==' '|| i==-1){
head=i+1;
printWord(ch, head);
}
i--;
}
//printf("\n%d",i);
//printf("\n%s",ch);
return 0;
}
//Print the word, till meet the blank
void printWord(char *ch, int head){
int i=head;
while(ch[i]!=' '&&ch[i]!='\0'){
printf("%c",ch[i]);
i++;
}
printf(" ");
}