#include<stdio.h>#include<string.h>intmain(){
char s[205], a, b;fgets(s,205,stdin);// 读入带空格的字符串,最多可以读入204个字符scanf("%c %c",&a,&b);// 输入a和bint len =strlen(s);for(int i =0; i < len;++i){
if(s[i]== a)
s[i]= b;}printf("%s", s);// 输出字符串return0;}
2047: [Example 5.16] Filter spaces
#include<stdio.h>#include<string.h>intmain(){
char s_in[205], s_out[205];fgets(s_in,205,stdin);// 读入带空格的字符串,最多可以读入204个字符int len =strlen(s_in), bn =0, si =0;// len:输入字符串的长度,bn:空格数量计数,si:输出字符串的索引for(int i =0; i <= len;++i)// 遍历到字符串结束标志'\0'位置{
if(s_in[i]==' '){
bn++;// 空格数量计数加1if(bn ==1)
s_out[si++]= s_in[i];// 将第一个空格填充到输出字符串中}else{
s_out[si++]= s_in[i];// 将非空格字符填充到输出字符串中
bn =0;// 重置空格数量计数}}printf("%s", s_out);// 输出字符串return0;}
2048: [Example 5.18] String sorting
#include<stdio.h>#include<string.h>voidswap(char*a,char*b){
char temp[25];strcpy(temp, a);strcpy(a, b);strcpy(b, temp);}intmain(){
char s[25][25];int n;scanf("%d",&n);// 输入nfor(int i =0; i < n;++i){
scanf("%s", s[i]);// 输入字符串}for(int i =0; i < n -1;++i){
// 选择排序int m = i;// 找从i+1~n最小值下标for(int j = i +1; j < n;++j){
if(strcmp(s[j], s[m])<0){
m = j;}}swap(s[m], s[i]);// 交换最小值与当前值}for(int i =0; i < n;++i){
printf("%s\n", s[i]);// 输出排序后的字符串}return0;}
#include<stdio.h>#include<string.h>#include<ctype.h>intmain(){
char s[260];fgets(s,sizeof(s),stdin);// 使用fgets函数读取输入字符串,可以读取空格和换行符int sum =0, len =strlen(s);for(int i =0; i < len;++i){
if(isdigit(s[i]))// 使用isdigit函数判断字符是否为数字
sum++;}printf("%d", sum);return0;}
1130: Find the first character that appears only once
#include<stdio.h>#include<string.h>intmain(){
int a[128]={
};// 数组a[字母]表示该字母出现的次数,初始化为0char s[100005];scanf("%s", s);int len =strlen(s);for(int i =0; i < len;++i)
a[s[i]]++;// 统计每个字符出现的次数for(int i =0; i < len;++i)// 遍历字符串,找到第一个只出现一次的字符{
if(a[s[i]]==1){
printf("%c", s[i]);return0;}}printf("no");// 没找到return0;}
1131: Genetic correlation
#include<stdio.h>#include<string.h>intmain(){
double y;// 阈值char s1[505], s2[505];// 输入的两个字符串scanf("%lf%s%s",&y, s1, s2);// 读取阈值和两个字符串int len =strlen(s1);// 字符串长度int ct =0;// 计数// 遍历字符串,比较每个位置上的字符是否相等for(int i =0; i < len;++i){
if(s1[i]== s2[i])
ct++;// 相等则计数加1}// 计算相等字符的比例并与阈值比较if((double)ct / len >= y)printf("yes");elseprintf("no");return0;}
1132: Rock Paper Scissors
#include<stdio.h>#include<string.h>intmain(){
char s1[10], s2[10];int n;scanf("%d",&n);for(int i =0; i < n;++i){
scanf("%s %s", s1, s2);if(strcmp(s1, s2)==0)printf("Tie\n");else{
if(strcmp(s1,"Rock")==0){
if(strcmp(s2,"Scissors")==0)printf("Player1\n");elseprintf("Player2\n");}if(strcmp(s1,"Scissors")==0){
if(strcmp(s2,"Paper")==0)printf("Player1\n");elseprintf("Player2\n");}if(strcmp(s1,"Paper")==0){
if(strcmp(s2,"Rock")==0)printf("Player1\n");elseprintf("Player2\n");}}}return0;}
1133: Output friends and family strings
#include<stdio.h>#include<string.h>intmain(){
char s1[105], s2[105];fgets(s1,105,stdin);// 从标准输入读取一行字符串,存储在s1中int len =strlen(s1);// 计算字符串s1的长度for(int i =0; i < len;++i)
s2[i]= s1[i]+ s1[(i +1)% len];// 将s1中每个字符与下一个字符相加,存储在s2中
s2[len]='\0';// 在s2的末尾添加\0,构成字符串printf("%s", s2);// 输出s2return0;}
1134: Legal C identifier check
#include<stdio.h>#include<string.h>intmain(){
char s[25];scanf("%s", s);// 从标准输入读取字符串,存储在s中if(s[0]>='0'&& s[0]<='9')// 如果第一个字符是数字{
printf("no");return0;}int len =strlen(s);// 计算字符串s的长度for(int i =0; i < len;++i){
// 如果出现不满足要求的字符if(!(s[i]>='0'&& s[i]<='9'||
s[i]>='a'&& s[i]<='z'||
s[i]>='A'&& s[i]<='Z'||
s[i]=='_')){
printf("no");return0;}}printf("yes");return0;}
#include<stdio.h>#include<string.h>intmain(){
char s[85], r[85];fgets(s,sizeof(s),stdin);int len =strlen(s);for(int i =0; i < len;++i){
if((s[i]>='a'&& s[i]<='y')||(s[i]>='A'&& s[i]<='Y'))
r[i]= s[i]+1;elseif(s[i]=='Z'|| s[i]=='z')
r[i]= s[i]-25;else
r[i]= s[i];}
r[len]='\0';printf("%s\n", r);return0;}
1137: Encrypted medical record
#include<stdio.h>#include<string.h>#include<ctype.h>voidreverse(char s[])//倒序{
int len =strlen(s);for(int i =0; i < len/2;++i){
// 交换位置char temp = s[i];
s[i]= s[len -1- i];
s[len -1- i]= temp;}}voidchangeCase(char s[])//大小写转换{
int len =strlen(s);for(int i =0; i < len;++i){
if(isupper(s[i]))// 大写字母转小写字母
s[i]=tolower(s[i]);elseif(islower(s[i]))// 小写字母转大写字母
s[i]=toupper(s[i]);}}voidmoveRight(char s[])//字母右移3位{
int len =strlen(s);for(int i =0; i < len;++i){
if(isupper(s[i]))// 大写字母右移
s[i]=(s[i]-'A'+3)%26+'A';elseif(islower(s[i]))// 小写字母右移
s[i]=(s[i]-'a'+3)%26+'a';}}intmain(){
char s[55];scanf("%s", s);// 获取输入的字符串moveRight(s);// 字母右移3位changeCase(s);// 大小写转换reverse(s);// 倒序printf("%s\n", s);// 输出处理后的字符串return0;}
1138: Convert lowercase letters in a string to uppercase letters
#include<stdio.h>#include<string.h>#include<ctype.h>intmain(){
char s[105];fgets(s,105,stdin);// 获取输入的字符串,包括空格int len =strlen(s)-1;// 减去换行符的长度for(int i =0; i < len;++i){
if(s[i]>='a'&& s[i]<='z')// 判断字符是否是小写字母
s[i]= s[i]+'A'-'a';// 转换为大写字母}printf("%s", s);// 输出处理后的字符串return0;}