我想實現一個簡單的目標,即將字符串傳遞給函數並返回包含有關該字符串的數據的結構。我寫了這個,但是當我嘗試編譯它時,我得到一個運行時錯誤,我不明白爲什麼。我很感謝你看看。傳遞字符串函數並返回一個結構
#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;
}
@Ganesh我錯了。他正在返回結構體的值。沒關係。 – 2013-04-05 10:34:55
@Koushik ..謝謝..刪除我的評論 – Ganesh 2013-04-05 10:36:17