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