我的程序在下面反转一个字符串,而不是通过字符而不是字.
示例输入 Hello Mello Sanple输出 Hello Mello Hello
我不知道我从哪里得到第一个Hello.
#include "stdafx.h"
#include <iostream>
int main(int argc, char **argv)
{
char input[255];
char *head; // head of a word.
char *tail; // tail of a word.
printf("Enter a sentence: ");
gets_s(input); // read a whole line.
tail = input + strlen(input) - 1;
while (tail >= input)
{
if (*tail == 0 || *tail == ' ')
{
tail--; // move to the tail of a word.
}
else
{
tail[1] = 0;
head = tail;
while (head >= input)
{
if (head == input || *(head - 1) == ' ')
{
printf("%s",input); // output a word.
printf(" ");
tail = head - 1; // seek the next word.
break;
}
head--; // move to the head of a word.
}
}
}
printf("\n\n");
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以得到任何帮助吗?
我认为你的意思是打印头,而不是输入.您是从字符串的开头打印,而不是从刚刚找到的单词的开头打印.
printf("%s",head); // output a word.
Run Code Online (Sandbox Code Playgroud)