c语言的一个指针题,,,很重要!!!求大神帮帮忙 抄笔记的时候不认真,,,程序运行不出来,

2024年12月02日 11:29
有1个网友回答
网友(1):

根据你的题目, 我重新给你写了完整的程序,你可以参考下。

源代码如下(在vc++ 6.0下编译通过):

#include
#include
#include

#define __DEBUG_PRINT

#define MAX_STR_LEN 30

void count(char *str)
{
int number_count = 0;
int non_number_char_count = 0;

while (*str != '\0')
{
if (*str >= '0' && *str <= '9')
{
number_count++;
}
else
{
if ( (*str > 'z' || *str < 'a')
&& (*str > 'Z' || *str < 'A') )
{
non_number_char_count++;
}
}

str++;
}

printf("The count of number is: %d\n", number_count);
printf("The count of non number and non charactor is: %d\n", non_number_char_count);

return;
}

int main(void)
{
int i = 1;
int str_num = 0;
char **pStr = NULL;
int tmp = 0;

printf("Pls input the string number(<=100): ");

scanf("%d", &str_num);

pStr = (char **)malloc(str_num * sizeof(char));

printf("pls input each string.\n");

tmp = str_num;
while (tmp--)
{
*pStr = (char *)malloc(MAX_STR_LEN * sizeof(char));
printf("string%d: \n", i++);
scanf("%s", *pStr);
pStr += MAX_STR_LEN;
}

pStr -= MAX_STR_LEN * str_num;

#ifdef __DEBUG_PRINT
i = 1;
tmp = str_num;
printf("The following are your input: \n");
while (tmp--)
{
printf("string%d: %s\n", i++, *pStr);
pStr += MAX_STR_LEN;
}
pStr -= MAX_STR_LEN * str_num;
#endif

printf("The following is the result: \n");

i = 1;
tmp = str_num;
while (tmp--)
{
printf("string%d: \n", i++);
count(*pStr);
free(*pStr);
pStr += MAX_STR_LEN;
}

free(pStr);

return 0;
}