c语言怎么让字母倒序排列尼,如何倒序单词顺序输出 ? 我是没辙了
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
#include
#include
/* 测试(33个字符): you can cage a swallow can't you? */
int main(void)
{
char word, sentence[] = "you can cage a swallow can't you?";
int count = 0;
/*
while((word = getchar()) != '\n')
{
sentence[count] = word;
count++;
}
*/
/* 添加结束符 */
// sentence[count] = '\0';
/* 含结束符: 33位字符 */
int length = strlen(sentence);
count = length - 1;
/* 创建数组用于存储反向输出结果 */
char temp[length];
for(int i = 0; 0 <= count; i++, count--)
{
temp[i] = sentence[count];
printf("sentence的当前字符: %c temp当前字符: %c\n", sentence[count], temp[i]);
}
/* 添加结束符 */
temp[length] = '\0';
printf("temp 数组输出: %s\n", temp);
char storage[length];
int i, j = 0, mark = 0, pre = 0;
for(i = 0; i < length; i++) // i用于遍历temp数组
{
if(temp[i] == ' '|| temp[i] == '\0')
{
mark = i - 1; // make 记录字母下标
// printf("三个标记的位置: %d %d %d\n", i, mark, prv);
for(; pre < mark; j++)
{
printf("循环后mark的位置: %d\n", mark);
storage[j] = temp[mark];
printf("temp数组当前字符: %c, storage数组当前字符: %c\n", temp[mark], storage[j]);
mark--;
}
pre = i; // 重置前置位置
storage[++j] = ' ';
}
}
// storage[j] = '\0';
printf("反转结果: %s\n", storage);
}
/* strlen() 不包含结束符'\0' 但 sizeof() 包含结束符 '\0' */
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
