【问题标题】:Dynamically allocating memory for a string of arbitrary length为任意长度的字符串动态分配内存
【发布时间】:2020-01-29 06:29:12
【问题描述】:

我想创建一个函数来计算字符串 str 中字符 c 的出现次数,无论字符串中字符 c 是大写还是小写。我正在尝试使用 toupper 和 tolower 功能,但它不起作用。

在主函数中,我想使用 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

【问题讨论】:

  • Hannah McDermott,很好奇,为什么在 printf("%lu\n", strlen(a_str)); 中使用 "%lu" 而不是 "%zu"

标签: c string function dynamic allocation


【解决方案1】:

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

代码

#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, '$'));

}

【讨论】:

  • a_str2 的内存泄漏。没有 0 个终止符。关于行尾的假设。为什么要与 toupper()tolower() 进行比较?为什么不只是 toupper() 起始字符,然后您只需要与字符串中的 toupper() 进行比较。完全忽略了原始错误的来源。只提供没有解释的工作代码不是一个好的答案!
猜你喜欢
  • 1970-01-01
  • 2018-06-16
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多