C语言-实验9-字符串
本系列将以一个学习者的眼光,从零基础一步一步学会最基础的C语言编程,本文将讲解C语言第九个板块:字符串。
字符串是C语言中处理文本数据的核心,也是编程中最常用的数据形式之一。和其他语言不同,C语言没有专门的字符串类型,而是通过字符数组 + 结束符的方式实现。这篇博客会从零基础视角,用通俗的解释和可直接运行的示例代码,帮你掌握字符串的定义、操作和常用库函数。
字符串的认识
你可以把字符串想象成“一串按顺序排列的字符珠子”,比如 "hello" 就是由 h、e、l、l、o 这几颗珠子串起来的。
- C语言中,字符串必须以空字符
\0结尾(\0的ASCII值为0),它是字符串结束的标志,占用1个字节但不计入字符串长度。 - 字符串本质是字符数组,但普通字符数组不一定是字符串(只有包含
\0的字符数组才是字符串)。
在 C 语言中,字符串的核心价值:
- 处理文本输入输出
- 实现文本处理逻辑
- 与指针结合
字符串的定义
1.字符串的定义方式
C语言中定义字符串主要有3种方式,核心是确保包含 \0:
// 方式1:直接用双引号赋值(推荐,编译器自动添加\0)char str1[] = "hello";// 方式2:手动初始化字符数组(需手动加\0)char str2[] = {'h', 'e', 'l', 'l', 'o', '\0'};// 方式3:字符指针指向字符串常量(不可修改内容)char *str3 = "hello";2.字符串的关键操作
- 长度计算:使用
strlen()函数(头文件string.h),只计算有效字符,不包含\0。 - 输入输出:
printf("%s", str)/scanf("%s", str)。 - 库函数操作:拼接
strcat()、复制strcpy()、比较strcmp()(均需string.h)。
示例
1.字符串的基础定义与长度计算
#include <stdio.h>#include <string.h>
int main() { char str1[] = "hello world"; char str2[] = {'h', 'i'}; printf("字符串str1:%s\n", str1); printf("str1的有效长度(strlen):%zu\n", strlen(str1)); printf("str1的数组字节数(sizeof):%zu\n", sizeof(str1)); printf("str2的strlen长度:%zu(随机值,因无\\0)\n", strlen(str2)); return 0;}输出:
字符串str1:hello worldstr1的有效长度(strlen):11str1的数组字节数(sizeof):12str2的strlen长度:15(随机值,因无\0)
2.字符串的输入与输出
#include <stdio.h>#include <string.h>
int main() { char name[20]; char msg[50]; printf("请输入你的名字(无空格):"); scanf("%s", name); printf("你好,%s!\n", name); getchar(); printf("请输入一句想讲的话(可带空格):"); fgets(msg, sizeof(msg), stdin); msg[strcspn(msg, "\n")] = '\0'; printf("你输入的内容是:%s\n", msg); printf("这句话的长度是:%zu\n", strlen(msg)); return 0;}输入:
张三C语言真有趣
输出:
请输入你的名字(无空格):你好,张三!请输入一句想讲的话(可带空格):你输入的内容是:C语言真有趣这句话的长度是:6
3.字符串常用库函数
#include <stdio.h>#include <string.h>
int main() { char str1[20]; strcpy(str1, "hello"); printf("复制后str1:%s\n", str1); strcat(str1, " world"); printf("拼接后str1:%s\n", str1); char str2[] = "hello"; char str3[] = "world"; int cmp1 = strcmp(str1, str2); int cmp2 = strcmp(str2, str3); printf("str1和str2比较结果:%d\n", cmp1); printf("str2和str3比较结果:%d\n", cmp2); char *p = strchr(str1, 'w'); if (p != NULL) { printf("找到字符'w',位置:%ld\n", p - str1); } return 0;}输出:
复制后str1:hello拼接后str1:hello worldstr1和str2比较结果:32str2和str3比较结果:-15找到字符'w',位置:6
4.指针操作字符串
#include <stdio.h>#include <string.h>
int main() { char str[] = "C language"; char *p = str; printf("指针遍历字符串:"); while (*p != '\0') { printf("%c", *p); p++; } printf("\n"); p = str; *(p + 2) = '_'; printf("修改后字符串:%s\n", str); return 0;}输出:
指针遍历字符串:C language修改后字符串:C_language
应用
7-1 sdut-C语言-字符编码
请将一串长度最长为5的纯字母文本译成一个密码,密码规律如下:用原来的字母后面的第4个字母代替原来的字母。如C用G代替(如果该字母没后面没有第4个字母可代替,则需从26字母表中的首字母开始代替,比如:W用A代替,Z用D代替),最后得到的文本即为密码。
输入格式:输入一串文本,长度最大为5。
输出格式:输出对应的密码。格式为:
password is 密码
输入示例:
China输出示例:
password is Glmre
7-1 解答:
#include <stdio.h>
int main() { char password[6]; int i = 0; while (i < 5 && scanf("%c", &password[i]) == 1) { if ((password[i] >= 'A' && password[i] <= 'Z') || (password[i] >= 'a' && password[i] <= 'z')) { if (password[i] >= 'A' && password[i] <= 'Z') { password[i] = (password[i] - 'A' + 4) % 26 + 'A'; } else if (password[i] >= 'a' && password[i] <= 'z') { password[i] = (password[i] - 'a' + 4) % 26 + 'a'; } i++; } } password[i] = '\0'; printf("password is %s\n", password); return 0;}7-2 sdut - C语言实验-保留字母
编一个程序,输入一个允许带空格的字符串,将组成字符串的所有非英文字母的字符删除后输出。
输入格式:一个字符串,长度不超过80个字符。
输出格式:删掉非英文字母后的字符串。
输入示例:
abc123+xyz.5输出示例:
abcxyz
7-2 解答:
#include <stdio.h>#include <ctype.h>
int main() { char str[81]; char result[81] = {0}; int j = 0; fgets(str, 81, stdin); for (int i = 0; str[i] != '\0'; i++) { if (isalpha(str[i])) { result[j++] = str[i]; } } printf("%s\n", result); return 0;}7-3 sdut-C语言实验- 大小写转换
输入一字符串,请把一串字符中的小写字母变成大写字符,大写字母变成小写字母,其他的保持不变。
输入格式:输入有多组。
每组输入一个字符串,长度不大于80,不包含空格。
输出格式:输出转换后的字符串
输入示例:
A*B+输出示例:
a*b+
7-3 解答:
#include <stdio.h>
int main() { char str[81]; while (scanf("%s", str) != EOF) { for (int i = 0; str[i] != '\0'; i++) { if (str[i] >= 'A' && str[i] <= 'Z') { str[i] += 32; } else if (str[i] >= 'a' && str[i] <= 'z') { str[i] -= 32; } } printf("%s\n", str); } return 0;}7-4 sdut-C语言实验- 字符串分割
输入一个字符串,要求把这个字符串按照某个分隔符来分割成若干个字符串输出。
输入格式:输入数据有多组(数据组数不超过 100),到 EOF 结束。
每组数据输入一行,格式为 “s c”,其中 s 为一个不含空格且长度不超过 1000 的字符串,表示待分割的字符串;c 为一个不是空格的字符,表示分隔符。
输入数据保证在待分割的字符串中,分隔符至少出现一次且不会出现在字符串开头或末尾,并且不会出现连续多个分隔符的情况。
输出格式:对于每组数据,输出分割后的字符串,每个字符串占一行。
输入示例:
23,DE ,123.a,/45/6.8 /输出示例:
123DE0123.a,456.8
7-4 解答:
#include <stdio.h>#include <string.h>
int main() { char buf[1005]; while (fgets(buf, sizeof(buf), stdin) != NULL) { buf[strcspn(buf, "\n")] = '\0'; int space_pos = -1; int len = strlen(buf); for (int i = 0; i < len; i++) { if (buf[i] == ' ') { space_pos = i; break; } } if (space_pos == -1 || space_pos + 1 >= len) { puts(buf); continue; } char sep = buf[space_pos + 1]; int split_len = space_pos; int start = 0; for (int i = 0; i < split_len; i++) { if (buf[i] == sep) { printf("%.*s\n", i - start, buf + start); start = i + 1; } } printf("%.*s\n", split_len - start, buf + start); } return 0;}7-5 sdut- C语言实验-删除指定字符
从键盘输入一个字符串给str和一个字符给c,删除str中的所有字符c并输出删除后的字符串str。
输入格式:第一行是一个字符串,不超过100个字符 ,可以带空格;
第二行是一个字符。
输出格式:删除指定字符后的字符串。
输入示例:
sdf$$$sdf$$$输出示例:
sdfsdf
7-5 解答:
#include <stdio.h>#include <string.h>
int main(){ char arr[101]; fgets(arr, sizeof(arr), stdin); char c; scanf("%c",&c); int len=strlen(arr); if (len > 0 && arr[len - 1] == '\n') { arr[len - 1] = '\0'; len--; } for(int i=0;i<len;i++){ if(arr[i]!=c){ printf("%c",arr[i]); } } return 0;}7-6 sdut-C语言实验- 全字母句
全字母句 (pangram) 指包含字母表中全部 26 个英文字母(不区分大小写)的句子,其常被用于展示英文字体的显示效果。
输入多个句子,判断哪些句子是全字母句。
注意:句子是可以带空格的。
输入格式:输入数据有多组(数据组数不超过 100),到 EOF 结束。
每组数据包含一行长度不超过 100 的字符串。
输出格式:对于每组数据,输出一行。
如果是全字母句则输出 “Yes”,否则输出 “No”(不包括引号)。
输入示例:
The quick brown fox jumps over the lazy dog.The 6th ACM Funny Programming For/While Contest输出示例:
YesNo
7-6 解答:
#include <stdio.h>#include <string.h>
int main() { char str[101]; int exist[26];
while (fgets(str, sizeof(str), stdin) != NULL) { memset(exist, 0, sizeof(exist)); for (int i = 0; str[i] != '\0'; i++) { char c = str[i]; if (c >= 'A' && c <= 'Z') { c += 32; } if (c >= 'a' && c <= 'z') { exist[c - 'a'] = 1; } } int is_pangram = 1; for (int i = 0; i < 26; i++) { if (exist[i] == 0) { is_pangram = 0; break; } } printf("%s\n", is_pangram ? "Yes" : "No"); } return 0;}7-7 sdut-C语言实验- 字符统计1
给出一串字符,要求统计出里面的字母、数字、空格以及其他字符的个数。
数字:0, 1, …, 9
空格:” “(不包括引号)
剩下的可打印字符全为其他字符。
输入格式:测试数据有多组。
每组数据为一行(长度不超过100000)。
数据至文件结束(EOF)为止。
输出格式:每组输入对应一行输出。
包括四个整数a b c d,分别代表字母、数字、空格和其他字符的个数。
输入示例:
A0 ,输出示例:
1 1 1 1
7-7 解答:
#include <stdio.h>#include <ctype.h>
int main() { int MAX_LEN=100001; char line[MAX_LEN]; while (fgets(line, MAX_LEN, stdin) != NULL) { int letters = 0, digits = 0, spaces = 0, others = 0; int i = 0; while (line[i] != '\0') { if (isalpha(line[i])) { letters++; } else if (isdigit(line[i])) { digits++; } else if (line[i] == ' ') { spaces++; } else if (line[i] != '\n') { others++; } i++; } printf("%d %d %d %d\n", letters, digits, spaces, others); } return 0;}7-8 sdut-C语言实验- 字符统计2
输入英文句子,输出该句子中除了空格外出现次数最多的字符及其出现的次数。
输入格式:输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行。
输出格式:逐行输出每个句子中出现次数最多的字符及其出现的次数(如果有多个字符的次数相同,只输出ASCII码最小的字符)。
输入示例:
I am a studenta good programming problemABCD abcd ABCD abcd输出示例:
a 2o 4A 2
7-8 解答:
#include <stdio.h>#include <string.h>
int main() { int MAX_LEN=101; int ASCII_SIZE=128; char line[MAX_LEN]; int count[ASCII_SIZE]; while (fgets(line, MAX_LEN, stdin) != NULL) { memset(count, 0, sizeof(count)); int i = 0; while (line[i] != '\0') { if (line[i] != ' ' && line[i] != '\n') { count[(unsigned char)line[i]]++; } i++; } int max_count = 0; char target_char = 0; for (int j = 0; j < ASCII_SIZE; j++) { if (count[j] > max_count) { max_count = count[j]; target_char = (char)j; } } printf("%c %d\n", target_char, max_count); } return 0;}7-9 sdut-C语言实验-虎子查找指定字符
山东理工大学与爱尔兰利莫瑞克大学是中外合作院校,许多优秀的学长学姐通过自己努力获得了留学爱尔兰的机会。
虎子作为优秀的学长已经在爱村求学半年了,也认识了很多国外的同学,他们为了更加熟悉,会玩一个从从某个同学的名字中查找某指定的字符的游戏。
现在请你帮助虎子编写一个程序,从输入的英文名字中查找指定的字符。
输入格式:输入的第一行是一个待查找的字符。第二行是一个以回车结束的非空字符串(不超过30个字符)。
输出格式:如果找到,在一行内按照格式“index = 下标”输出该字符在字符串中所对应的最小下标(下标从0开始);否则输出”Not Found”。
输入示例1:
MMaggie Ma输出示例1:
index = 0输入示例2:
aKen Thompson输出示例2:
Not Found
7-9 解答:
#include <stdio.h>
int main(){ char arr[31]; char a; int b=-1; scanf(" %c",&a); getchar(); fgets(arr, sizeof(arr), stdin); for(int i=0;i<31;i++){ if(arr[i]==a){ b=i; break; }else{ b=-1; } } if(b!=-1){ printf("index = %d",b); }else{ printf("Not Found"); } return 0;}7-10 sdut-C语言实验- 简单字符串比较
请使用字符串比较函数,比较两个字符串的大小,并按要求输出比较后的结果。字符串最长不超过15个字符。
输入两个字符串str1和str2,如果第一个字符串与第二个字符串相等,输出str1=str2,如果第一个字符串大于第二个字符串,输出str1>str2,如果第一个字符串小于第二个字符串,输出str1 < str2。
输入格式:第1行为第一个字符串。
第2行为第二个字符串。
输出格式:在一行输出比较后的结果。例如”abc”与”abc”相等,输出为abc=abc,如果”ab”小于”abc”,输出ab < abc。
输入示例:
ababc输出示例:
ab<abc
7-10 解答:
#include <stdio.h>#include <string.h>
int main() { char str1[16], str2[16]; fgets(str1, sizeof(str1), stdin); fgets(str2, sizeof(str2), stdin); if (str1[strcspn(str1, "\n")] == '\n') { str1[strcspn(str1, "\n")] = '\0'; } if (str2[strcspn(str2, "\n")] == '\n') { str2[strcspn(str2, "\n")] = '\0'; } int cmp_result = strcmp(str1, str2); if (cmp_result == 0) { printf("%s=%s\n", str1, str2); } else if (cmp_result > 0) { printf("%s>%s\n", str1, str2); } else { printf("%s<%s\n", str1, str2); }
return 0;}7-11 sdut-C语言实验- 虎子找老乡
新的学年开始了,学校又迎来了一批朝气蓬勃的大一新生。虎子很想认识新来的老乡,于是他拿到了计算机学院大一新生的名单,上面有n个人的信息(姓名和地址),虎子想知道有多少人是他的老乡以及老乡的名字。
输入格式:多组输入,每组的第一行是一个整数n(1<=n<=100),表示名单上人的数量。
接下来一行有一个字符串表示LeiQ的地址(1<=len<=20)
接下来n行,每行两个字符串,第一个是姓名,第二个是地址。
输出格式:先输出老乡的名字(按照输入的顺序),最后输出老乡的人数。
如果没有找到老乡,输出:Sorry.
输入示例:
4LaiyangXiaoming LaiyangXiaohong HezeXiaohuang LaiwuXiaoguang Laiyang输出示例:
XiaomingXiaoguang2
7-11 解答:
#include <stdio.h>#include <string.h>
int main() { int n; while (scanf("%d", &n) != EOF) { char huzi_addr[21]; getchar(); fgets(huzi_addr, 21, stdin); huzi_addr[strcspn(huzi_addr, "\n")] = '\0'; char name[100][21]; int count = 0; for (int i = 0; i < n; i++) { char cur_name[21], cur_addr[21]; scanf("%s %s", cur_name, cur_addr); if (strcmp(cur_addr, huzi_addr) == 0) { strcpy(name[count], cur_name); count++; } } if (count == 0) { printf("Sorry.\n"); } else { for (int i = 0; i < count; i++) { printf("%s\n", name[i]); } printf("%d\n", count); } } return 0;}7-12 sdut-C语言实验- 虎子认识新朋友之字符串排序
虎子寒假加入了一个实验室,认识了3个新同学,他决定将同学的名字按字母顺序存储到自己的电子通讯录上。
你知道如何编程实现吗?
请编程实现:输入3个字符串,按字典序从小到大进行排序。
输入格式:输入数据有一行,分别为3个字符串,用空格分隔,每个字符串长度不超过100。。
输出格式:输出排序后的三个字符串,用空格分隔。
输入示例:
abcd cdef bcde输出示例:
abcd bcde cdef
7-12 解答:
#include <stdio.h>#include <string.h>
int main() { char s1[101], s2[101], s3[101]; char temp[101]; scanf("%s %s %s", s1, s2, s3); if (strcmp(s1, s2) > 0) { strcpy(temp, s1); strcpy(s1, s2); strcpy(s2, temp); } if (strcmp(s2, s3) > 0) { strcpy(temp, s2); strcpy(s2, s3); strcpy(s3, temp); if (strcmp(s1, s2) > 0) { strcpy(temp, s1); strcpy(s1, s2); strcpy(s2, temp); } } printf("%s %s %s\n", s1, s2, s3); return 0;}7-13 sdut-C语言实验- 简单字符串排序
从键盘输入10个学生的姓名和成绩,请按字典序排列学生的姓名并输出(姓名和成绩对应关系保持不变)。
输入格式:输入共11行,前10行每行是一个学生的姓名,最后一行是10个用空格分开的整数表示对应的10个学生成绩。(姓名大小不超过20个字符)
输出格式:输出姓名按字典序排列后的学生姓名和成绩,共10行,每个学生的姓名和成绩占一行,姓名和成绩间用逗号分开。
输入示例:
BushWhiteMarkJeanBlackWoodJennyFrankBillSmith78 85 96 65 46 83 77 88 54 98输出示例:
Bill,54Black,46Bush,78Frank,88Jean,65Jenny,77Mark,96Smith,98White,85Wood,83
7-13 解答:
#include <stdio.h>#include <string.h>
int main() { char names[10][21]; int scores[10]; char temp_name[21]; int temp_score; for (int i = 0; i < 10; i++) { fgets(names[i], 21, stdin); names[i][strcspn(names[i], "\n")] = '\0'; } for (int i = 0; i < 10; i++) { scanf("%d", &scores[i]); } for (int i = 0; i < 9; i++) { for (int j = 0; j < 9 - i; j++) { if (strcmp(names[j], names[j+1]) > 0) { strcpy(temp_name, names[j]); strcpy(names[j], names[j+1]); strcpy(names[j+1], temp_name); temp_score = scores[j]; scores[j] = scores[j+1]; scores[j+1] = temp_score; } } } for (int i = 0; i < 10; i++) { printf("%s,%d\n", names[i], scores[i]); } return 0;}7-14 英文单词排序
本题要求编写程序,输入若干英文单词,对这些单词按长度从小到大排序后输出。如果长度相同,按照输入的顺序不变。
输入格式:输入为若干英文单词,每行一个,以#作为输入结束标志。其中英文单词总数不超过20个,英文单词为长度小于10的仅由小写英文字母组成的字符串。
输出格式:输出为排序后的结果,每个单词后面都额外输出一个空格。
输入示例:
blueredyellowgreenpurple#输出示例:
red blue green yellow purple
7-14 解答:
#include<stdio.h>#include<string.h>int main(){ char s[20][10]; char t[10]; char ch[1]={'#'}; int h=0; scanf("%s",s[h]); while(strcmp(s[h],ch)!=0) { h++; scanf("%s",s[h]); }
for(int i=0;i<h;i++) { for(int j=i+1;j<h;j++) { if(strlen(s[i])>strlen(s[j])) { strcpy(t,s[i]); strcpy(s[i],s[j]); strcpy(s[j],t); } } } for(int i=0;i<h;i++) { printf("%s ",s[i]); } return 0;}7-15 倒数第N个字符串
给定一个完全由小写英文字母组成的字符串等差递增序列,该序列中的每个字符串的长度固定为 L,从 L 个 a 开始,以 1 为步长递增。例如当 L 为 3 时,序列为 { aaa, aab, aac, …, aaz, aba, abb, …, abz, …, zzz }。这个序列的倒数第27个字符串就是 zyz。对于任意给定的 L,本题要求你给出对应序列倒数第 N 个字符串。
输入格式:输入在一行中给出两个正整数 L(2 ≤ L ≤ 6)和 N(≤10)。
输出格式:在一行中输出对应序列倒数第 N 个字符串。题目保证这个字符串是存在的。
输入示例:
3 7417输出示例:
pat
7-15 解答:
#include <stdio.h>#include <math.h>
int main() { int L, N; scanf("%d %d", &L, &N); long long total = 1; for (int i = 0; i < L; i++) { total *= 26; } long long pos = total - N; char res[7] = {0}; for (int i = L-1; i >= 0; i--) { res[i] = 'a' + (pos % 26); pos /= 26; } printf("%s\n", res); return 0;}7-16 sdut-C语言实验- 简单编码
将一串文本译成密码,密码的规律是:
将原来的小写字母全部翻译成大写字母,大写字母全部翻译成小写字母,数字的翻译规律如下:
0——>9
1——>8
2——>7
3——>6
4——>5
5——>4
6——>3
7——>2
8——>1
9——>0
然后将所有字符的顺序颠倒。
输入格式:输入一串文本,最大字符个数不超过100。
输出格式:输出编码后的结果。
输入示例:
china输出示例:
ANIHC
7-16 解答:
#include <stdio.h>
int main(){ char string[102]; scanf("%s",string); int i = 0; while(string[i]!='\0'){ if(string[i]>='a'&&string[i]<='z'){ string[i]=string[i]-32; }else if(string[i]>='A'&&string[i]<='Z'){ string[i]=string[i]+32; }else if(string[i]>='0'&&string[i]<='9'){ string[i]='9'-(string[i]-'0'); } i++; } for(int j=i-1;j>=0;j--){ printf("%c",string[j]); } return 0;}7-17 sdut-C语言实验- 编码
给你一个由大写字母组成的组成的字符串,你可以用如下规则对其进行编码:
1、 包含K个相同字母的连续字符串可以用KX表示,其中X是相同的字母。
2、 如果K为1,不输出K。
输入格式:输入有多组,直到文件结束。每组一个字符串,长度为10000以内。
输出格式:输出编码后的字符串。
输入示例:
ABCABBCCC输出示例:
ABCA2B3C
7-17 解答:
#include <stdio.h>#include <string.h>
int main() { char str[10001]; while (scanf("%s", str) != EOF) { int len = strlen(str); if (len == 0) { printf("\n"); continue; } char current = str[0]; int count = 1; for (int i = 1; i < len; i++) { if (str[i] == current) { count++; } else { if (count > 1) { printf("%d%c", count, current); } else { printf("%c", current); } current = str[i]; count = 1; } } if (count > 1) { printf("%d%c", count, current); } else { printf("%c", current); } printf("\n"); } return 0;}7-18 sdut-C语言实验-字符串逆序
将一个字符串str的内容颠倒过来,并输出。str的长度不超过100个字符。
输入格式:输入包括一行。
第一行输入的字符串。
输出格式:输出转换好的逆序字符串。。
输入示例:
I am a student输出示例:
tneduts a ma I
7-18 解答:
#include <stdio.h>#include <string.h>
int main() { char str[101]; fgets(str, 101, stdin); str[strcspn(str, "\n")] = '\0'; int len = strlen(str); for (int i = len - 1; i >= 0; i--) { putchar(str[i]); } putchar('\n'); return 0;}7-19 sdut-C语言实验-回文串的判断
输入一个字符串(可以包含空格),判断该字符串是否为回文,只考虑字母字符,字母的大小写没有区别。
回文就是字符串中心对称,从左向右读和从右向左读的内容是一样的。
输入格式:输入一串字符(长度小于100)。
输出格式:若该串字符是回文串输出“yes”,否则输出“no”。
输入示例:
level输出示例:
yes
7-19 解答:
#include <stdio.h>#include <string.h>#include <ctype.h>
int main(){ char a[101],tmp[101]; fgets(a, 101, stdin);
int t = 0; for(int i=0; a[i]!='\0'; i++){ if(isalpha(a[i])){ tmp[t++] = tolower(a[i]); } } tmp[t] = '\0'; int b = t; if (b > 0 && a[b-1] == '\n') { a[b-1] = '\0'; b--; } int is=1; for(int i=0;i<b/2;i++){ if(tmp[i]!=tmp[b-i-1]){ is=0; break; } } if(is){ printf("yes"); }else{ printf("no"); } return 0;}7-20 sdut-C语言实验- 简单密码破解
假设虎子的一个BBS上的密码为zvbo941987,为了方便记忆,他通过一种算法把这个密码变换成YUANzi1987,这个密码是他的名字和出生年份,怎么忘都忘不了,而且可以明目张胆地放在显眼的地方而不被别人知道真正的密码。
他是这么变换的,大家都知道老年人手机上的字母: 1—1, abc—2, def—3, ghi—4, jkl—5, mno—6, pqrs—7, tuv—8 wxyz—9, 0—0,就这么简单,虎子把密码中出现的小写字母都变成对应的数字,数字和其他的符号都不做变换,声明:密码中没有空格,而密码中出现的大写字母则变成小写之后往后移一位,如:X,先边成小写,再往后移一位,不就是y了嘛,简单吧。记住,z往后移是a哦。
输入格式:输入包括多个测试数据。输入是一个明文,密码长度不超过100个字符,输入直到文件结尾。
输出格式:输出渊子真正的密文。
输入示例:
YUANzi1987输出示例:
zvbo941987
7-20 解答:
#include <stdio.h>#include <ctype.h>#include <string.h>
int main() { char plain[101]; while (fgets(plain, 101, stdin) != NULL) { plain[strcspn(plain, "\n")] = '\0'; int len = strlen(plain); for (int i = 0; i < len; i++) { if (isupper(plain[i])) { char lower = tolower(plain[i]); char converted = (lower == 'z') ? 'a' : (lower + 1); putchar(converted); } else if (islower(plain[i])) { char c = plain[i]; if (c >= 'a' && c <= 'c') putchar('2'); else if (c >= 'd' && c <= 'f') putchar('3'); else if (c >= 'g' && c <= 'i') putchar('4'); else if (c >= 'j' && c <= 'l') putchar('5'); else if (c >= 'm' && c <= 'o') putchar('6'); else if (c >= 'p' && c <= 's') putchar('7'); else if (c >= 't' && c <= 'v') putchar('8'); else if (c >= 'w' && c <= 'z') putchar('9'); } else { putchar(plain[i]); } } putchar('\n'); } return 0;}7-21 sdut-C语言实验- 简单密码破解
英文辅音字母是除A、E、I、O、U以外的字母。本题要求编写程序,统计给定字符串中大写辅音字母的个数。
输入格式:输入在一行中给出一个不超过80个字符、并以回车结束的字符串。
输出格式:输出在一行中给出字符串中大写辅音字母的个数。
输入示例:
HELLO World!输出示例:
4
7-21 解答:
#include <stdio.h>
int main() { char c; int count = 0; while ((c = getchar()) != '\n') { if (c >= 'A' && c <= 'Z') { if (c != 'A' && c != 'E' && c != 'I' && c != 'O' && c != 'U') { count++; } } } printf("%d\n", count); return 0;}7-22 sdut- C语言实验-单词统计
从键盘输入一行字符(长度小于100),统计其中单词的个数,各单词以空格分隔,且空格数可以是多个。
输入格式:输入只有一行句子。仅有空格和英文字母构成。
输出格式:单词的个数。
输入示例:
stable marriage problem Consists of Matching members输出示例:
7
7-22 解答:
#include <stdio.h>#include <string.h>
int main() { char str[101]; fgets(str, 101, stdin); str[strcspn(str, "\n")] = '\0'; int count = 0; int in_word = 0; int len = strlen(str); for (int i = 0; i < len; i++) { if (str[i] != ' ') { if (!in_word) { count++; in_word = 1; } } else { in_word = 0; } } printf("%d\n", count); return 0;}7-23 字母串
英语老师要求学生按照如下规则写一串字母:
如果写了某个大写字母,下一个就必须写同个字母的小写,或者写字母表中下一个字母的大写;
如果写了某个小写字母,下一个就必须写同个字母的大写,或者写字母表中前一个字母的小写;
当然也可以什么都不写,就结束这个字母串。
例如 aAaABCDdcbBC 就是一个合法的字母串;而 dEFfeFGhI 就是非法的。注意 a 没有前一个字母, Z 也没有下一个字母。
现在面对全班学生交上来的作业,老师请你写个程序自动批改。
输入格式:输入在第一行给出一个不超过 100 的正整数 N。随后 N 行,每行给出一位学生的作业,即仅由英文字母组成的非空字母串,长度不超过2×10。
输出格式:对每位学生的作业,如果正确就在一行中输出 Y,否则输出 N。
输入示例:
2aAaABCDdcbBCdEFfeFGhI输出示例:
YN
7-23 解答:
#include <stdio.h>#include <ctype.h>
int main() { int N; if (scanf("%d", &N) != 1) return 0; while (getchar() != '\n'); for (int i = 0; i < N; i++) { int is_valid = 1; char curr, next; curr = getchar(); while (curr != '\n' && curr != EOF) { next = getchar(); if (next == '\n' || next == EOF) { break; } if (isupper(curr)) { int case1 = (next == tolower(curr)); int case2 = (curr != 'Z' && next == curr + 1 && isupper(next)); if (!case1 && !case2) { is_valid = 0; while (getchar() != '\n'); break; } } else if (islower(curr)) { int case1 = (next == toupper(curr)); int case2 = (curr != 'a' && next == curr - 1 && islower(next)); if (!case1 && !case2) { is_valid = 0; while (getchar() != '\n'); break; } } else { is_valid = 0; break; } curr = next; } printf("%c\n", is_valid ? 'Y' : 'N'); } return 0;}7-24 考试座位号
每个 PAT 考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位。正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座位就座。但有些考生迟到了,试机已经结束,他们只能拿着领到的试机座位号码求助于你,从后台查出他们的考试座位号码。
输入格式:输入第一行给出一个正整数 N(≤1000),随后 N 行,每行给出一个考生的信息:准考证号 试机座位号 考试座位号。其中准考证号由 16 位数字组成,座位从 1 到 N 编号。输入保证每个人的准考证号都不同,并且任何时候都不会把两个人分配到同一个座位上。
考生信息之后,给出一个正整数 M(≤N),随后一行中给出 M 个待查询的试机座位号码,以空格分隔。
输出格式:对应每个需要查询的试机座位号码,在一行中输出对应考生的准考证号和考试座位号码,中间用 1 个空格分隔。
输入示例:
43310120150912233 2 43310120150912119 4 13310120150912126 1 33310120150912002 3 223 4输出示例:
3310120150912002 23310120150912119 1
7-24 解答:
#include <stdio.h>
typedef struct { char id[17]; int exam_seat;} Student;
int main() { int N; scanf("%d", &N); Student stu[N+1]; for (int i = 0; i < N; i++) { char id[17]; int test_seat, exam_seat; scanf("%s %d %d", id, &test_seat, &exam_seat); stu[test_seat].exam_seat = exam_seat; for (int j = 0; j < 16; j++) { stu[test_seat].id[j] = id[j]; } stu[test_seat].id[16] = '\0'; } int M; scanf("%d", &M); for (int i = 0; i < M; i++) { int query_seat; scanf("%d", &query_seat); printf("%s %d\n", stu[query_seat].id, stu[query_seat].exam_seat); } return 0;}7-25 A-B
本题要求你计算A−B。不过麻烦的是,A和B都是字符串 —— 即从字符串A中把字符串B所包含的字符全删掉,剩下的字符组成的就是字符串A−B。
输入格式:输入在2行中先后给出字符串A和B。两字符串的长度都不超过10,并且保证每个字符串都是由可见的ASCII码和空白字符组成,最后以换行符结束。
输出格式:在一行中打印出A−B的结果字符串。
输入示例:
I love GPLT! It's a fun game!aeiou输出示例:
I lv GPLT! It's fn gm!
7-25 解答:
#include<stdio.h>#include<string.h>
int main(){ char a[10010],b[10010]; fgets(a, sizeof(a), stdin); fgets(b, sizeof(b), stdin); a[strcspn(a, "\n")] = '\0'; b[strcspn(b, "\n")] = '\0'; int len_a = strlen(a); int len_b = strlen(b); for(int i=0; i<len_a; i++){ int flag=1; for(int j=0; j<len_b; j++){ if(a[i]==b[j]){ flag=0; break; } } if(flag){ printf("%c", a[i]); } } return 0;}7-26 检查密码
本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能。该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母、数字和小数点 .,还必须既有字母也有数字。
输入格式:输入第一行给出一个正整数 N(≤ 100),随后 N 行,每行给出一个用户设置的密码,为不超过 80 个字符的非空字符串,以回车结束。
注意: 题目保证不存在只有小数点的输入。
输出格式:对每个用户的密码,在一行中输出系统反馈信息,分以下5种:
如果密码合法,输出Your password is wan mei.;
如果密码太短,不论合法与否,都输出Your password is tai duan le.;
如果密码长度合法,但存在不合法字符,则输出Your password is tai luan le.;
如果密码长度合法,但只有字母没有数字,则输出Your password needs shu zi.;
如果密码长度合法,但只有数字没有字母,则输出Your password needs zi mu.。
输入示例:
5123szheshi.wodepw1234.5678WanMei23333pass*word.6输出示例:
Your password is tai duan le.Your password needs shu zi.Your password needs zi mu.Your password is wan mei.Your password is tai luan le.
7-26 解答:
#include<stdio.h>#include<string.h>
int main(){ int n; char s[82]; scanf("%d",&n); getchar(); for(int i=0;i<n;i++){ gets(s); int flag=0,word=0,num=0; if(strlen(s)<6){ printf("Your password is tai duan le.\n"); }else{ for(int j=0;s[j]!='\0';j++){ if((s[j]>='a'&&s[j]<='z')||(s[j]>='A'&&s[j]<='Z')||(s[j]>='0'&&s[j]<='9')||s[j]=='.'){ flag=0; }else{ printf("Your password is tai luan le.\n"); flag=1; break; } if((s[j]>='a'&&s[j]<='z')||(s[j]>='A'&&s[j]<='Z')){ word=1; } if(s[j]>='0'&&s[j]<='9'){ num=1; } } if(flag==0){ if(word==1&&num==0) printf("Your password needs shu zi.\n"); if(word==0&&num==1) printf("Your password needs zi mu.\n"); if(word==1&&num==1) printf("Your password is wan mei.\n"); } } } return 0;}总结
OK了,今天你学会了C语言程序的字符串及其相关知识点!一起加油吧!
支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!