c语言字符串的小程序,C语言----------字符串小程序练习(二)

1、给定程序中,函数fun的功能是:找出形参s所指字符串中出现频率最高的字母(不区分大小写),并统计出其出现的次数。例如,形参s所指的字符串为:abcAbsmaxless,程序执行后的输出结果为:letter 'a' : 3 times  letter 's' : 3 times

#include

#define len 100

char *fun(char *s)

{

int max = 0;

int i, j;

int a[len] = {0};

for(i = j = 0; s[i]; i++)

{

for(j = i-1; j >= 0; j--)

{

if((s[j] == s[i]) || (s[j] == s[i] - 32) || (s[j] == s[i] + 32))

a[j] += 1;

}

if(j < 0)

a[i] = 1;

}

for(i = 0; i < len; i++)

{

if(a[max] < a[i])

max = i;

}

for(i = 0; i < len; i++)

{

if(a[max] == a[i])

printf("%c: %d\n", s[i], a[i]);

}

}

int main(void)

{

char str[len];

printf("please input a string:\n");

scanf("%s", str);

fun(str);

return 0;

}

2、自己编写程序实现void itoa(int x, char *str)(将数字x,转换为字符串并保存到str中)

#include

#include

void itoa(int x, char *s)

{

int i, cnt = 0;

int bas = x, flag = 0;

while(bas / 10)

{

bas /= 10;

cnt++;

}

if(x < 0)

{

s[0] = '-';

x = -x;

flag = 1;

cnt++;

}

for(i = cnt; i >= flag; i--)

{

s[i] =x % 10 + '0';

x /= 10;

}

s[cnt + 1] = '\0';

}

int main(void)

{

int n;

char str[100];

printf("please input a num:");

scanf("%d", &n);

itoa(n, str);

printf("%s\n", str);

return 0;

}

3、编写统计某字符串的字符长度:int my_strlen(char str[]);

#include

int my_strlen(char *str)

{

int cnt = 0;

while(*str)

{

cnt++;

str++;

}

return cnt;

}

int main(void)

{

char str[100];

printf("please input a string:\n");

scanf("%s", str);

printf("%d\n", my_strlen(str));

return 0;

}

4、用循环结构将一个字符数组里的值复制到另外一个字符数组里(main函数里实现)。

1)、练习使用strcpy函数

2)、将上题设计思想整理成my_strcpy函数,并在main函数里调用、测试。

3)、部分复制,例如从第3个字符开始复制

#include

char *my_strcpy(char *dest, const char *src)

{

char *add = dest;

while(*src)

*dest++ = *src++;

*dest = '\0';

return add;

}

int main(void)

{

char *a = "hello";

char b[100];

printf("%s\n", my_strcpy(b, a));

return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值