编程练习4.25

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



 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值