推扬网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
推扬网 门户 你问我答 查看内容

怎么用C语言实现逆向打印,比如我先输入“百”,再输入“度”,让度打印在百的前面, ...

2020-7-18 16:32| 发布者: admin| 查看: 119| 评论: 0

摘要: Frank_Zhou991的回答 #include #define BUFFER_SIZE 1024// 获取字符串长度int countChar(const char *str, int capacity) {int count = 0;int i = 0;for (; i capacity; ++i) {if (str == 0) {break;}++count;}ret ...

Frank_Zhou991的回答

#include

#define BUFFER_SIZE 1024

// 获取字符串长度
int countChar(const char *str, int capacity) {
int count = 0;
int i = 0;

for (; i < capacity; ++i) {
if (str[i] == 0) {
break;
}
++count;
}
return count;
}

// 反转字符串
void reverse(char *str, int len) {
int i = 0;
int j = len - 1;
char temp;

while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;

++i;
--j;
}
}


int main() {

char buf[BUFFER_SIZE] = {0};

printf("请输入:\n");
scanf("%s", buf);

reverse(buf, countChar(buf, BUFFER_SIZE));

printf("倒置后:\n");
printf("%s\n", buf);

}


注:这个程序只能正常显示英文字母、阿拉伯数字和英文标点符号这种单字节字符,处理中文这种多字节字符会乱码。

Koaboe的回答

可以将两次输入的结果存在两个变量里,然后输出的时候就可以换顺序输出

鲜花

握手

雷人

路过

鸡蛋

最新评论

热门推荐
最新资讯

广告服务|投稿要求|禁言标准|版权说明|免责声明|手机版|小黑屋|推扬网 ( 粤ICP备18134897号 )|网站地图 | 邮箱:vayae@hotmail.com

GMT+8, 2024-3-29 18:33 , Processed in 0.337549 second(s), 28 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

返回顶部