我试图实现将字符串传递给函数并返回包含有关该字符串的数据的结构的简单目标。我已经写了这个,但是当我尝试编译它时,我得到一个运行时错误,我不明白为什么。我很感谢你看一看。
#include <stdio.h>
#include <ctype.h>
#include <string.h>
struct stringStats
{
int length;
int uppercase;
int lowercase;
int digits;
int nonAlphaNum;
};
struct stringStats stringReader (char anyString[])
{
int i;
struct stringStats returned = {0, 0, 0, 0, 0 };
returned.length = strlen(anyString);
for (i = 0; anyString[i] != '\0'; ++i)
{
if (isupper(anyString[i]))
++returned.uppercase;
if (islower(anyString[i]))
++returned.lowercase;
if (isdigit(anyString[i]))
++returned.digits;
if (isalnum(anyString[i]) == 0)
++returned.nonAlphaNum;
}
return returned;
}
int main(void)
{
struct stringStats stored;
char passedString[] = "Th1s string's g0t it all!";
stored = stringReader(passedString);
printf ("%i\n%i\n%i\n%i\n%i\n", stored.length, stored.uppercase, stored.lowercase,
stored.digits, stored.nonAlphaNum);
return 0;
}