在一行上输入一个长度为
、由大小写字母和数字混合构成的字符串
。
在一行上输出处理后的字符串
。
Jkdi234klowe90a3
Jkdi*234*klowe*90*a*3*
#include <stdio.h> #include <ctype.h> int main() { char ch; char last; int flag = 0; while ((ch = getchar()) != '\n') { // 进入数字区,或离开数字区去字母区 if ((isdigit(ch) && flag == 0) || (!isdigit(ch) && flag != 0)) { flag = !flag; printf("*%c", ch); } // 数字区内或字母区 else { printf("%c", ch); } last = ch; } // 如果数字结尾,补全末尾 if (isdigit(last)) { printf("*"); } return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> void func(char arr[], int len); int main() { int len = 0; char arr[101]; memset(arr, '\0', sizeof(arr)); fgets(arr, sizeof(arr), stdin); // 去掉换行符 arr[strcspn(arr, "\n")] = '\0'; len = strlen(arr); if (len < 1 || len > 100) exit(-1); func(arr, len); return 0; } void func(char arr[], int len) { if (len < 1 || len > 100) exit(-1); char arr_temp[200]; int j = 0; // j 指针用于 arr_temp int i = 0; // i 指针用于原始 arr for (i = 0; i < len; i++) { if (len == 1) { printf("%c", arr[i]); break; } if (isdigit(arr[i])) { arr_temp[j++] = '*'; // 在 arr_temp 中添加 '*' arr_temp[j++] = arr[i]; // 添加当前字符 // 处理连续数字 while (i + 1 < len && isdigit(arr[i + 1])) { i++; // 向后移动,处理下一个数字 arr_temp[j++] = arr[i]; // 添加这个数字 } arr_temp[j++] = '*'; // 添加 '*' 结束数字部分 } else { arr_temp[j++] = arr[i]; // 处理非数字字符 } } arr_temp[j] = '\0'; // 确保字符串以 null 结束 printf("%s", arr_temp); // 输出生成的字符串 }
//每次保存前一个字符,在字母和数字中间加*即可。需要额外判断结尾是否为数字 #include <stdio.h> int main() { char c, ch = 127; while ((c = getchar()) != EOF) { if (c > '9' || c < '0') { if (ch <= '9' && ch >= '0') { printf("*"); } printf("%c", c); ch = c; } else if (c <= '9' && c >= '0') { if (ch > '9' || ch < '0') { printf("*"); } ch = c; printf("%c", c); } if (c == '\n') { if (ch <= '9' && ch >= '0') printf("*\n"); break; } } return 0; }
#include <stdio.h> int main() { char arr[100]; gets(arr); int len = strlen(arr); for(int i=0;i<len;i++){ if(arr[i]>='0'&&arr[i]<='9'){ printf("*"); while(arr[i]>='0'&&arr[i]<='9'){ printf("%c", arr[i]); i++; } i--; printf("*"); }else{ printf("%c", arr[i]); } } return 0; }
#include <ctype.h> #include <stdio.h> #include <string.h> int main() { char buffer[128]; int len; scanf("%s", buffer); len = strlen(buffer); if(isdigit(buffer[0])){ printf("*"); } /* 找数字和字母之间的跳变沿 */ for(int i=0;i<len;i++){ int flagP = !!isdigit(buffer[i]); int flagA = !!isdigit(buffer[i+1]); printf("%c", buffer[i]); if(flagP + flagA == 1){ printf("*"); } } return 0; }
#include <stdio.h> int main() { char str[200] = {0}; char tmp; char i = 0; char isStart = 1; while(scanf("%c", &tmp) != EOF) { if(tmp == '\n'){ if (str[i] >= '0' && str[i] <= '9') str[++i] = '*'; break; } if((i == 0) && (tmp >= '0' && tmp <= '9') && isStart){ str[i] = '*'; str[++i] = tmp; isStart = 0; continue; } else if((i == 0) && (tmp < '0' || tmp > '9') && isStart){ str[i] = tmp; isStart = 0; continue; } if(((str[i] >= '0') && (str[i] <= '9')) && (tmp < '0' || tmp > '9')) { str[++i] = '*'; str[++i] = tmp; continue; } if((str[i] < '0' || str[i] > '9' ) && (tmp >= '0' && tmp <= '9')) { str[++i] = '*'; str[++i] = tmp; continue; } str[++i] = tmp; } printf("%s\n", str); return 0; }
我要是会python该多好
#include <stdio.h> #include <ctype.h> int main() { char str[101] = {0}, *cur = str; scanf("%s", str); while (*cur) { if (isdigit(*cur) && (cur == str || !isdigit(*(cur - 1)))) printf("*"); printf("%c", *cur); if (isdigit(*cur) && (*(cur + 1) == 0 || !isdigit(*(cur + 1)))) printf("*"); cur++; } return 0; }
#include <stdio.h> #include <stdlib.h> typedef struct list { char ch; struct list *next; }list; int main() { list *p0,*p1,*p2; char temp; p2 = p1 = p0 = (list*)malloc(sizeof(list)); scanf("%c", &temp); while (temp != '\n') { p1 = p2; p1->ch = temp; p1->next = malloc(sizeof(list)); p2 = p1->next; scanf("%c", &temp); } p1->next=NULL; p1=p0; p2=p1->next; if(p1->ch>='0'&&p1->ch<='9') { p0=malloc(sizeof(list)); p0->ch='*'; p0->next=p1; } while(p2!=NULL) { if((!(p1->ch>='0'&&p1->ch<='9')&&(p2->ch>='0'&&p2->ch<='9'))|| ((p1->ch>='0'&&p1->ch<='9')&&!(p2->ch>='0'&&p2->ch<='9'))) { p1->next=(list*)malloc(sizeof(list)); p1->next->ch='*'; p1->next->next=p2; } p1=p2; p2=p2->next; } if(p1->ch>='0'&&p1->ch<='9') { p1->next=(list*)malloc(sizeof(list)); p1->next->ch='*'; p1->next->next=NULL; } p1=p0; while(p1!=NULL) { printf("%c",p1->ch); p1=p1->next; } return 0; }
#include<stdio.h> #include<ctype.h> #include<string.h> int main(void) { //定义两个字符数组,ch用于存放原数据,temp用于存放改变后的数据 char ch[101],temp[121]; gets(ch); //获取原数据 int j = 0,i,t=0; //t用于记录连续的数字的个数 for (i = 0; ch[i]!='\0'; i++) //遍历原数组 { if (ch[i] >= '0' && ch[i] <= '9') //当遇到数字字符时 { t+=1; //t记录连续的数字个数,并加一 int flag=1; //记录该数字的左侧或右侧是否被改变 //判断数字字符的左侧是否为非数字 if (!isdigit(ch[i - 1])) { temp[j++] = '*'; temp[j++] = ch[i]; flag=0;//左侧被改变 } //判断数字字符的右侧是否为非数字 if (!isdigit(ch[i + 1])) { if(t==1) //连续部分只有一个数字时 { temp[j++]='*'; } else { temp[j++]=ch[i]; temp[j++]='*'; } flag=0;//右侧被改变 } if(flag) temp[j++]=ch[i]; //未被处理时 } else { temp[j++] = ch[i]; //不是数字字符时 t=0; //t归零,表示数字连续部分结束,下次重新计数 } } //在temp的最后一个有效字符后加上'\0',构成字符串,便于输出 temp[j] = '\0'; puts(temp);//输出结果 return 0; }
#include<stdio.h> #include<stdlib.h> #include<string.h> int main() { int mark = 0; char str[101] = {'\0'}; while (~scanf("%s", str)) { int len = strlen(str); for (int i = 0; i < len; i++) { if (str[i] >= '0' && str[i] <= '9' && mark == 0) { printf("*"); mark = 1; } else if ((str[i] < '0') || (str[i] > '9') && (mark == 1)) { printf("*"); mark = 0; } printf("%c", str[i]); if (i == (len - 1) && str[i] >= '0' && str[i] <= '9') printf("*\n"); } } }