一个字符串的问题
比如输入一个字符串my name is tom
输出
tom is name my
scanf输入遇到空格就当'\0'处理结束了,不知道该怎么办
只能使用scanf输入
各位大哥给点解题思路吧,那些空格要怎么处理呢?(新手一枚,各位大哥不用想那么复杂,帮我分析一下就好)
[ 本帖最后由 w851777025 于 2014-2-2 01:13 编辑 ]
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define NUMBER_WORDS 100 // 單詞極限數目
#define LENGTH_WORD 30 // 單詞極限長度
int main(void)
{
char words[NUMBER_WORDS][LENGTH_WORD] = { "" };
int number = 0;
char* p;
// 輸入文本,有沒有回車都可以,祇有遇到'.'字符才結束循環
while ((number < NUMBER_WORDS) && (scanf_s("%s", words[number], LENGTH_WORD) == 1))
{
// 查找單詞最後一個字符是否'.'字符,若是則抹掉它,然後結束讀取循環
p = strrchr(words[number++], '.');
if (p != NULL)
{
*p = '\0';
break;
}
}
// 逆序輸出讀入的文本
printf_s("\n\n結果:\n");
for (int index = number; index > 0; --index)
{
printf_s("%s ", words[index - 1]);
}
printf_s("\npress any key to continue...");
_getch();
return EXIT_SUCCESS;
}

