gpt4 book ai didi

c - 使用 Malloc 输入字符串后程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 07:59:05 26 4
gpt4 key购买 nike

<分区>

输入长字符串后程序崩溃。我认为该程序无法处理用户输入的大量字符串。你能帮我看看我的代码有什么问题吗?

一个简单的 C 程序,使用指针计算字符串中的元音和辅音。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char * sEnteredString= NULL;
int i;
int sizeString;
int vowel=0;
int consonants=0;

printf("A program that counts the number of vowels and consonants in a string\n");
printf("Enter a string:\t");
sEnteredString = (char*)malloc(sizeof(sEnteredString));
scanf(" %s",sEnteredString);

if (sEnteredString==NULL)
{
printf("OUT OF MEMORY\n");
}
else
{
sizeString=strlen(sEnteredString);

for (i=0; i<sizeString;i++)
{
if((sEnteredString[i] == 'A') || \
(sEnteredString[i] == 'a') || \
(sEnteredString[i] == 'E') || \
(sEnteredString[i] == 'e') || \
(sEnteredString[i] == 'I') || \
(sEnteredString[i] == 'i') || \
(sEnteredString[i] == 'O') || \
(sEnteredString[i] == 'o') || \
(sEnteredString[i] == 'U') || \
(sEnteredString[i] == 'u'))
{
vowel ++;
}
else
{
consonants ++;
}
}
printf("Entered vowel: %d \nEntered constants: %d\n", vowel, consonants);
}

free(sEnteredString);
return 0;
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com