Ironic enhanced version
Given a word of English, asking you to write a program, all the words of the sentence order reversed output.
Input formats:
Test input comprises a test case, given the string length does not exceed a total of 500,000 in a row. String composed of several words and a number of spaces, where the word is English letters (case is case) consisting of a string, a number between words separated by spaces.
Output formats:
Each test case output per line, output sentence after the reverse, and ensure that only one space between words.
Write topic and ideas: In the beginning I wanted to write a two-dimensional array, the results do half did not get out;
only fought other ideas think it should be replaced with a pointer to the array, and then was left out (a little embarrassed), and finally it no way, I can only be found a bit of code, learn about the big brother.
I decided to look at to understand the code, go back to thinking about their own ideas, try to use their own ideas to write again.
code show as below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char str[500010];
int len,real,head,count=0;
int i,j,k;
gets(str); //gets只有遇到回车才停止输入;
len=strlen(str); //len用来存储字符串长度;
for(i=len-1;i>=0;i--) //从后往前进行
{
if(str[i]!=' ')
{
real=i; //记录单词最后一个字母的位置
for(j=i-1;j>=0;j--)
{
if(str[j]==' ') //向前遍历,遇到空格停下来
{
break;
}
}
head=j+1; //指向空格的下一个空间,即单词的第一个字母
if(count>0) //如果不是输出第一个单词,即原字符串最后一个单词,要输出一个相隔的空格
{
printf(" ");
}
for(k=head; k<=real; k++)//从该单词的开头字符逐个输出到末尾字符
{
printf("%c", str[k]);
}
count++;
i=j; //从遇到的空格那个位置开始,继续向前查询
}
}
return 0;
}