
c语言的一个指针题,,,很重要!!!求大神帮帮忙 抄笔记的时候不认真,,,程序运行不出来, 20
编写一个函数Count实现统计一个字符串中数字字符个数及非字母数字字符(既不是字母也不是数字字符)个数,如字符串world%123#@21中数字字符个数为5、非字母数字字...
编写一个函数Count实现统计一个字符串中数字字符个数及非字母数字字符(既不是字母也不是数字字符)个数,如字符串world%123#@21中数字字符个数为5、非字母数字字符个数为3,对字符串的访问用指针实现。main函数中从键盘输入n(n<100)个字符串(每个字符串长度小于30),调用Count函数计算并输出所有字符串中数字字符的个数之和及非字母数字字符的个数之和。Input
输入格式:第一个整数为字符串个数n,后续为n个字符串。
Output
两个个数之间用一个空格分隔。
Sample Input3 an23shan?%:class For(i=2;i<10;i++) a="">10&&a<=30)
Sample Output9 18-------------------------------------#includevoid count(char[],int*,int*);int main(){ int n,s1=0,s2=0,m1,m2,i; char str[100]; scanf("%d",&n); for(i=0;i='0'&&*p<'9') else="" p="">='a'&&*p<='z'||*p>='A'&&*p<='Z') ; else (*s2)++; }} 展开
输入格式:第一个整数为字符串个数n,后续为n个字符串。
Output
两个个数之间用一个空格分隔。
Sample Input3 an23shan?%:class For(i=2;i<10;i++) a="">10&&a<=30)
Sample Output9 18-------------------------------------#includevoid count(char[],int*,int*);int main(){ int n,s1=0,s2=0,m1,m2,i; char str[100]; scanf("%d",&n); for(i=0;i='0'&&*p<'9') else="" p="">='a'&&*p<='z'||*p>='A'&&*p<='Z') ; else (*s2)++; }} 展开
展开全部
根据你的题目, 我重新给你写了完整的程序,你可以参考下。
源代码如下(在vc++ 6.0下编译通过):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}
源代码如下(在vc++ 6.0下编译通过):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询