怎么用C语言实现逆向打印,比如我先输入“百”,再输入“度”,让度打印在百的前面, ...
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的回答
可以将两次输入的结果存在两个变量里,然后输出的时候就可以换顺序输出经验分享互联网动态
更多阅读推荐