c++:输入一个字符串,分别统计字符串中,ASCII值为奇数和偶数的字符个数并输出
展开全部
int main()
{
char str[1024] = {0};
int len = 0;
char ch = 0;
while((ch = getchar()) != '\n')
{
str[len++] = ch;
}
char tmp1[1024] = {0};
char tmp2[1024] = {0};
int count1 = 0;
int count2 = 0;
for(int i = 0; i < len; i ++)
{
if(str[i] % 2)
tmp1[count1++] = str[i];
else
tmp2[count2++] = str[i];
}
printf("ASCII值为奇数的字符个数是:%d", count1);
for(int i = 0; i < count1; i ++)
printf(" %c ", tmp1[i]);
printf("ASCII值为偶数的字符个数是:%d", count2);
for(int i = 0; i < count2; i ++)
printf(" %c ", tmp2[i]);
return 0;
}
{
char str[1024] = {0};
int len = 0;
char ch = 0;
while((ch = getchar()) != '\n')
{
str[len++] = ch;
}
char tmp1[1024] = {0};
char tmp2[1024] = {0};
int count1 = 0;
int count2 = 0;
for(int i = 0; i < len; i ++)
{
if(str[i] % 2)
tmp1[count1++] = str[i];
else
tmp2[count2++] = str[i];
}
printf("ASCII值为奇数的字符个数是:%d", count1);
for(int i = 0; i < count1; i ++)
printf(" %c ", tmp1[i]);
printf("ASCII值为偶数的字符个数是:%d", count2);
for(int i = 0; i < count2; i ++)
printf(" %c ", tmp2[i]);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询