(3) 首先从键盘输入一任意字符串,然后将输入字符串中下标为偶数,且该字符的ASCII码为偶数的字符组合而成的一个新的字符串,最后输出新的字符串
#include <stdio.h>
#include <string.h>
int main(){
char str[1000];
char new_str[1000];
int i = 0;
int j = 0;
printf("请输入字符串\n");
scanf("%s",str);
for(i = 0 , j = 0; i < strlen(str) ; i += 2){
if(0 == (str[i] & 1)){ //判断当前字符是否为偶数
new_str[j] = str[i]; //拷贝当前字符于新字符串
j++; //新字符串写入索引更新
}
}
new_str[j] = '\0' //写入字符串结束符
printf("新字符串:%s\n",new_str);
}