C——字符串练习题
C——字符串练习
View Code
View Code
View Code
1.
查找一个字符在字符串1中出现的所有字符位置,
比如:
输入
asdfwd
d
输出为
3
6
输入
hhff
h
输出为
1
2
1 #include <string.h> 2 int main(int args,const char *argv[]) 3 { 4 char a[100] = "sgfgeasdefw"; 5 char target ; 6 scanf("%c",&target); 7 char *p = a; 8 while((p=strchr(p, target))) 9 { 10 printf("%ld\n",p-a+1); 11 p++; 12 } 13 }
2.
比较一个字符串2在字符串1中是否存在,如果存在输出Y,否则输出N
比如:
输入
asdfwd
dfw
输出为
Y
输入
sgfgeasdfw
hhff
输出为
N
1 #include <string.h> 2 int main(int args,const char *argv[]) 3 { 4 char a[100] = "sgfgeasdfw"; 5 const char *b = "ge"; 6 //传入的第二个参数,虽然是一个字符串,但是会自动解析位对应的每一个字符,之后判断a字符串中出现的第一个位置 7 //strtok(a, b); 8 //strstr(const char *,const char *);返回的是子字符串中出现的第一个位置 9 if(strstr(a, b)) 10 { 11 printf("Y"); 12 }else{ 13 printf("N"); 14 } 15 }
3.
查找一个字符串2在字符串1中出现的次数,
比如:
输入
asdfwd
d
输出为
2
输入
hhff
h
输出为
2
1 #include <string.h> 2 int main(int args,const char *argv[]) 3 { 4 char *src = "asfdasdfassdf"; 5 char target[100] = {}; 6 scanf("%s",target); 7 int cnt = 0; 8 char *p = src; 9 while((p=strstr(p, target))) 10 { 11 cnt++; 12 p++; 13 } 14 printf("出现的次数:%d",cnt); 15 return 0; 16 }
4.
给定一个任意字符串,然后将该字符串逆序输出。
比如:
输入
asdfwd
输出为
dwfdsa
1 #include <string.h> 2 char *reverse(char *a) 3 { 4 int len = (int)strlen(a); 5 char b[100] = {0}; 6 for(int i=len-1;i>=0;i--) 7 { 8 b[len-i-1] = a[i]; 9 } 10 char *res = b; 11 return res; 12 } 13 int main(int args,const char *argv[]) 14 { 15 char *a = "afdasfas"; 16 char *res = reverse(a); 17 printf("%s",res); 18 return 0; 19 }
5.
题目描述:“eeeeeaaaff" 压缩为 "e5a3f2",请编程实现
这题有一个比较尴尬的地方是,假如字符串是是“abc”,那么如果化成“a1b1c1”超过了原来字符串的长度,又题目给的是原地,我们假设字符串原长度就是题目给的那么长,那么就没办法了,所以如果遇到单个字母的后面不加1
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 5 void condense( char * src_str) 6 { 7 if(src_str==NULL) 8 return ; 9 10 int count=1; 11 int sub_length=1; 12 for(int i=1;i<strlen(src_str);i++) 13 { 14 if(src_str[i]==src_str[i-1]) 15 { 16 count++; 17 } 18 else 19 { 20 if(count==1) 21 { 22 src_str[sub_length++]=src_str[i]; //单个只要赋值 23 } 24 else 25 { 26 src_str[sub_length++]=count+48; //多个要加上数字,+48变成ascii码 27 src_str[sub_length++]=src_str[i]; 28 count=1; 29 } 30 } 31 } 32 33 if(sub_length<strlen(src_str)) 34 src_str[sub_length]='\0'; 35 36 printf("%s \n",src_str); 37 38 39 } 40 41 int main() 42 { 43 char str[]="abbbdffeeg"; 44 condense(str); 45 getchar(); 46 return 0; 47 }
6.题目描述:“eeeeeaaaff" 压缩为 "e5a3f2",并用一个字符串存储,通过子函数实现,结果用指针函数返回,请编程实现
1 #include <string.h> 2 #include <ctype.h> 3 char *getStringToCompress(char *src) 4 { 5 char compress[100] = {}; 6 int cnt = 0; 7 char *psrc = src; 8 char al[100] = {}; 9 int num[100] = {0}; 10 int i = 0; 11 while(*psrc) 12 { 13 cnt++; 14 if(*psrc != *(psrc+1)) 15 { 16 num[i] = cnt; 17 al[i] = *psrc; 18 cnt = 0; 19 i++; 20 } 21 psrc++; 22 } 23 int len = (int)strlen(al); 24 char buf[100] = ""; 25 //将保持在al和num数组中的元素转换成字符数组,保持到compress字符数组中 26 for(int h=0;h<len;h++) 27 { 28 if (isalpha(al[h])) { 29 sprintf(buf, "%c", al[h]); //利用sprintf(char *buf,const char *format,argument,...)将字符转位字符串类型 30 strcat(compress, buf); 31 } 32 if (!isalpha(num[h])) { 33 sprintf(buf, "%d", num[h]); //利用sprintf()函数将数字转换为 34 strcat(compress, buf); 35 } 36 } 37 char * res = compress; 38 return res; 39 } 40 int main(int args,const char *argv[]) 41 { 42 char src[100] = "eeeeeaaaaaaaaaaaff"; 43 char *compress = getStringToCompress(src); 44 printf("%s",compress); 45 return 0; 46 }