编程练习4.25
来源:互联网 发布:2k16捏脸数据 姚明 编辑:程序博客网 时间:2024/06/12 19:48
1. 统计一个字符串中个字符的百分比:如输入字符串asdfs a ----- 20%s ----- 40%d -----20%f -----20%
/*统计一个字符中个字符的百分比,如输入字符串asdfs,a--20% s--40%,d--20%,f--20%*/#include <stdio.h>#include <string.h>void fun(char *str,int a[]){int len = strlen(str); //记录字符串的长度int i = 0; char buffer[26]; //记录字符for(i = 0;i < 26;i++) //将每个单词的个数按顺序存放在a数组中{if(str[i] >= 'a'&&str[i] <= 'z'){a[str[i]-'a']++;buffer[str[i]-'a'] = str[i];}}for(i = 0;i < 26;i++){if(a[i] == 0){continue;}else{printf("%c----%d%\n",buffer[i],a[i]*100/len);}}}int main(){char str[100] = {0};int a[26] = {0};printf("please input string:\n");scanf("%s",str);fun(str,a);return 0;}
2.
,给一个字符串,有大小写字母,要求写一个函数把小写字母放在前面,大写字母放在后面,尽量使用最小空间,时间复杂度。(即用指针做)。
如:aAbBcCdD ---àabcdABCD
/*给一个字符串,有大小写字母,要求写一个函数把小写字母放在前面,大写字母放在后面,尽量使用最小的时间复杂度(即用指针做)如aAbBcCdD--->abcdABCD*/#include <stdio.h>#include <string.h>void my_move(char *str,char *result){int len = strlen(str);while(*str){if(*str >= 'a'&&*str <= 'z'){*result = *str;result++;}str++;}str = str-len; //指针重新指向首地址while(*str){if(*str >= 'A'&&*str <= 'Z'){*result = *str;result++;}str++;}*result = '\0';}int main(){char str[100] = {0};char result[100] = {0}; //存放结果printf("please input string:\n");scanf("%s",str);my_move(str,result);printf("the result is:%s\n",result);return 0;}
3,自我实现atoi(字符串转整形)
如:“123”转换成 123
“-123” 转换成 -123
/*自我实现atoi字符串转整形 “-123”转为-123*/#include <stdio.h>int my_atoi(char *str){int temp = 0;int flag = 1;if(*str == '-') //判断是否是否为负数{flag = -1;str++;}else{flag = 1;}while(*str){temp =temp*10 + (*str-'0');str++;}return flag*temp;}int main(){char str[100] = {0};int temp = 0;printf("please input str:\n");scanf("%s",str);temp = my_atoi(str);printf("the result is:%d\n",temp);}
4,自我实现itoa(整形转字符串)
如: 123 转换成 “123”
“-123” 转换成-123
/*自我实现整形转字符串 例如-123转为“-123”*/#include <stdio.h>#include <string.h>void my_itoa(int m,char *str){char temp;int count = 0; //记录字符串的长度int i = 0;if(m < 0) //当第一个字符为负数的情况{*str = '-';str++;while(-m){*str = (-m)%10+'0';str++;count++;m = m/10;}*str = '\0';str = str-count-1;for(i = 1;i < (count+1)/2;i++) //将字符串除了第一个,其他的逆序{temp = str[i];str[i] = str[count-i+1];str[count-i+1] = temp;}}else{while(m){*str = m%10+'0';str++;count++;m = m/10;}*str = '\0';str = str - count;for(i = 0;i < count/2;i++){temp = str[i];str[i] = str[count-i-1];str[count-i-1] = temp;}}}int main(){int num = 0;char str[100] = {0};char result[100] = {0};printf("please input the number:\n");scanf("%d",&num);my_itoa(num,str);printf("the result is :%s\n",str);}5、统计字符串中子串的个数。
/*输入一个字符串,计算字符串中字串中子串出现的次数*/#include <stdio.h>#include <string.h>int fun(char *src,char *b) //b为定义的子串,src为主串{int count = 0; //记录子串的个数char *ptr = src;char *tt = b; int k = 0; //记录相同字符的个数int len = strlen(b);while(*ptr != '\0'){while(*ptr == *tt&&*tt != '\0'){ptr++;tt++;k++;if(k == len) //长度为len时,计数器+1{count++;k = 0;ptr--;}}tt = b;ptr++;}return count;}int main(){char src[100];char src_son[50];int count;printf("please input string1:\n");scanf("%s",src);printf("please input string_son:\n");scanf("%s",src_son);count = fun(src,src_son);printf("the result:%d\n",count);return 0;}
0 0
- 编程练习4.25
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- 编程练习
- axure的使用实例1
- C++中rand()函数的用法
- 201412-2 Z字形扫描
- Java-使用java mail技术发送电子邮件
- Web应用防护系统OpenWAF开源CC防护模块
- 编程练习4.25
- Spring 基于 Aspect 注解的增强实现
- centos 上为火狐浏览器安装flash 插件
- 动态规划——字符串编辑问题
- 数据结构与算法之图的邻接表与邻接矩阵<十二>
- 基于 Django1.10 文档的深入学习(11)—— django.shortcuts 之 render()
- 容器或将代替私有云
- 数据结构与算法之图的深度遍历<十四>
- Java第一行代码