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;
}