C 为任意长度的字符串动态分配内存

C 为任意长度的字符串动态分配内存,c,string,function,dynamic,allocation,C,String,Function,Dynamic,Allocation,我想创建一个函数,计算字符串str中字符c的出现次数,不管字符串中字符c是大写还是小写。我试图使用toupper和tolower函数,但它不起作用 在main函数中,我希望使用malloc函数为最多50个字符的字符数组动态分配内存,然后使用fgets读取输入字符串。然后我想通过使用malloc函数为输入字符串正确分配内存,但要根据输入字符串的长度。然后我想将输入字符串复制到另一个大小合适的字符串中,并释放开头分配的内存。我不知道为什么,但是malloc函数在开始时没有分配50个字符。当我打印输入

我想创建一个函数,计算字符串str中字符c的出现次数,不管字符串中字符c是大写还是小写。我试图使用toupper和tolower函数,但它不起作用

在main函数中,我希望使用malloc函数为最多50个字符的字符数组动态分配内存,然后使用fgets读取输入字符串。然后我想通过使用malloc函数为输入字符串正确分配内存,但要根据输入字符串的长度。然后我想将输入字符串复制到另一个大小合适的字符串中,并释放开头分配的内存。我不知道为什么,但是malloc函数在开始时没有分配50个字符。当我打印输入字符串的长度时,它不会超过7个字符。我错过了什么

这就是我的代码的样子:

int count_insensitive(char *str, char c){
    int count = 0;
    for(int i = 0; str[i] != '\n'; i++){
        if(( str[i] == toupper(c)) || str[i] == tolower(c) ){
            count++;
        }     
    }
    return count;
}

int main(){
    char *a_str;
    a_str = (char *) malloc(sizeof(char) * 50);
    fgets(a_str, sizeof(a_str), stdin);
    printf("%lu\n", strlen(a_str));
    char *a_str2;
    a_str2 = (char *) malloc(sizeof(char) * (strlen(a_str)));
    strcpy(a_str2, a_str); 
    printf("%s\n", a_str2);
    free(a_str);

    printf("The character 'b' occurs %d times\n", count_insensitive(a_str2, 'b'));
    printf("The character 'H' occurs %d times\n", count_insensitive(a_str2, 'H'));
    printf("The character '8' occurs %d times\n", count_insensitive(a_str2, '8'));
    printf("The character 'u' occurs %d times\n", count_insensitive(a_str2, 'u'));
    printf("The character '$' occurs %d times\n", count_insensitive(a_str2, '$'));
如果我输入这个字符串:

Hello world hello world 
输出如下:

7
hello w
The character 'b' occurs 15 times
The character 'H' occurs 47 times
The character '8' occurs 18 times
The character 'u' occurs 17 times
The character '$' occurs 6 times

通过执行chux注释和一些小的更改,这就是代码的工作版本。 我会在接下来的几个小时里写一些解释

代码

#包括
#包括
#包括
#包括
int count_不敏感(char*str,char c){
整数计数=0;
对于(int i=0;str[i]!='\n';i++){
如果((str[i]==toupper(c))| str[i]==tolower(c)){
计数++;
}     
}
返回计数;
}
int main()
{
char*a_str;
a_str=malloc(50);
fgets(a_str,50,stdin);
printf(“%zu\n”,strlen(a_str));
字符*au str2;
a_str2=malloc(strlen(a_str));
strcpy(a_str2,a_str);
printf(“%s\n”,a_str2);
免费(a_str);
printf(“字符“b”出现了%d次\n”,计数不敏感(a_str2,'b');
printf(“字符'H'出现了%d次\n”,计数不敏感(a_str2,'H'));
printf(“字符'8'出现了%d次\n”,计数不敏感(a_str2,'8'));
printf(“字符'u'出现%d次\n”,计数不敏感(a_str2,'u'));
printf(“字符“$”出现了%d次\n”,计数不敏感(a_str2,$);
}

Hannah McDermott,好奇,为什么在
printf(“%lu\n”,strlen(a_str))中用
“%lu”编码
而不是
“%zu”
?a_str2
的内存泄漏。不允许使用0个终端。关于行尾的假设。为什么要与toupper()和tolower()比较?为什么不直接将起始字符
toupper()
与字符串中的
toupper()
进行比较呢。完全忽略原始文件中错误的来源。仅仅提供工作代码而不进行解释并不是一个好的答案!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

int count_insensitive(char *str, char c){
    int count = 0;
    for(int i = 0; str[i] != '\n'; i++){
        if(( str[i] == toupper(c)) || str[i] == tolower(c) ){
            count++;
        }     
    }
    return count;
}


int main()
{

    char *a_str;
    a_str = malloc(50);
    fgets(a_str, 50, stdin);
    printf("%zu\n", strlen(a_str));
    char *a_str2;
    a_str2 = malloc(strlen(a_str));
    strcpy(a_str2, a_str); 
    printf("%s\n", a_str2);
    free(a_str);

    printf("The character 'b' occurs %d times\n", count_insensitive(a_str2, 'b'));
    printf("The character 'H' occurs %d times\n", count_insensitive(a_str2, 'H'));
    printf("The character '8' occurs %d times\n", count_insensitive(a_str2, '8'));
    printf("The character 'u' occurs %d times\n", count_insensitive(a_str2, 'u'));
    printf("The character '$' occurs %d times\n", count_insensitive(a_str2, '$'));

}