题目是输入一个字符串,以单词为单位,以空格、逗号或句号为间隔,将字符串倒序输出(不包含标点符号)。
例如:
输入:hello world
输出:world hello
输入:hello,I am Tom.
输出:Tom am I hello
我的思路是输入一个字符串,再创建一个二维数组,将单词依次存储在二维数组的每个元素中,再逆序输出,中间间隔一个空格,但是编译通过后,输出有问题,请大家看看哪里错了,谢谢
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[101], s[20][20]; //str用于输入字符串,s用来存储字符串
int n, i, j, count;
scanf_s("%s", &str);
i = j = 0;
for (n = 0; n < strlen(str); n++)
{
if (str[n] == '\0')
{
break;
}
else if (str[n] != ' '&&str[n] != ','&&str[n] != '.')
{
s[i][j] = str[n];
j++; //将单词每个字母依次存入列中
}
else
{
i++; //记录单词个数
j = 0; //初始化列序数
}
}
count = i; //单词个数
for (i = count; i >= 0; i--)
{
for (j = 0; j < 20; j++)
{
printf("%c", s[i][j]); //倒序依次输出每个单词
}
printf(" ");
}
system("pause");
return 0;
}