题目:逆序一个中英混合字符串,此字符串包含字母字符、数字字符、空格字符、标点符号等英文字符以及中文字符,要求从键盘输入
源代码如下:
#include <stdio.h>
#define MAX 100 //定义字符串的最大长度
int main()
{
char ch[MAX];
int len = 0;
fgets(ch,MAX,stdin); //使用fgets从键盘获取一个字符串
while (ch[len])
{
len++;
}
ch[--len] = '\0'; //得到字符串长度,并去掉回车符
int min = 0;
int max = len - 1;
int temp;
while (min < max) //字符串逆序(按字节长度)
{
temp = ch[min];
ch[min++] = ch[max];
ch[max--] = temp;
}
int i = len;
while (i != 0) //从后开始扫描,遇到小于0的字符,则逆序自其往前三字符(UTF-8编码下中文字符占3个字节长度)
{
i--;
if (ch[i] < 0)
{
temp = ch[i];
ch[i] = ch[i - 2];
ch[i - 2] = temp;
i = i - 2;
}
}
printf (&