【发布时间】:2015-06-28 09:14:59
【问题描述】:
我正在尝试编写一个程序来反转整个字符串,并且还可以打印多次反转字符串中的每个单词。 例如我的字符串是: "2 狐狸跳过了那只懒狗。” 为此,输出应为: .god .god yzal yzal eht eht revo revo spmuj spmuj xoF xoF。 我试图将每个单词存储在二维数组中,然后反转每个单词,但我无法做到这一点。 这是我的代码。好心的帮助 注意:我们如何在控制台中提供 EOF
#include <stdio.h>
#include <stdlib.h>
int main() {
char string[100][100];
char ch;
int i = 0, j = 0, l = 0, count = 0, x = 0, y = 0;
while ((ch = getchar()) != EOF) {
string[i][l++] = ch;
if (ch == ' ') {
string[i][l] = '\n';
i++;
l = 0;
count++;
}
}
for (x = 0; x <= count; x++) {
int length = strlen(string[x]) - 1;
for (y = length; y >= 0; --y)
printf("%s", string[y]);
}
return 0;
}
【问题讨论】:
-
要在 Windows 上发送 EOF - 按 Ctrl+Z。要在类 Unix 系统上发送 EOF - 按 Ctrl+D。
-
在 Windows 上不是
Ctrl+M吗?