isalpha(字符串指针)的使用

Wizard_xxx 2015-12-10 08:59:56
统计key在source中出现次数 教材题
判断*ps是否为字母 书上用*ps的ASCII值 换成isalpha时报错。。
求解。。
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int sea(const char *ps,const char *pf) {
int i, count = 0;
char word[15];
while (*ps) {
i = 0;
while (isalpha(*ps)) //问题在这
//while ((*ps>='a'&&*ps<='z')) //问题在这
word[i++] = *ps++;
word[i] = '\0';
if (strcmp(pf, word) == 0)
count++;
ps++;
}
return count;
}
int main(void) {
char source[200];
char key[15];
gets_s(source);
gets_s(key);
printf("There are %d keys.", sea(source, key));
return 0;
}
...全文
170 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-12-22
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止。
lm_whales 2015-12-22
  • 打赏
  • 举报
回复
只要不包含 中文(等非单字节字符),以及,图形字符(>=128) 不用考虑 isapha 参数的符号问题 否则,是要关心一下的
lm_whales 2015-12-22
  • 打赏
  • 举报
回复
word[i++] = *ps++;///// word[i] = '\0'; if (strcmp(pf, word) == 0) count++; ps++;///这两个 ++ 会跳过结束符
c_flybird 2015-12-12
  • 打赏
  • 举报
回复
先试试楼上的回答,如果还不行的话改一下word的数组大小,因为ps指向的source有200个,word只有15.
fefe82 2015-12-10
  • 打赏
  • 举报
回复
isalpha((unsigned char)*ps) isalpha 的参数是个 int 。
C函数库手册,按照函数功能来分类 分类函数,所在函数库为ctype.h int isalpha(int ch) 若ch 是字母('A'-'Z','a'-'z')返回非0 值,否则返回0 int isalnum(int ch) 若ch 是字母('A'-'Z','a'-'z')或数字('0'-'9')返回非0 值,否则返回0 ...... 数学函数,所在函数库为math.h、stdlib.h、string.h、float.h int abs(int i) 返回整型参数i 的绝对值 double cabs(struct complex znum) 返回复数znum 的绝对值 double fabs(double x) 返回双精度参数x 的绝对值 ...... 目录函数,所在函数库为dir.h、dos.h int chdir(char *path) 使指定的目录path(如:"C:\\WPS")变成当前的工作目录,成 功返回0 int findfirst(char *pathname,struct ffblk *ffblk,int attrib)查找指定的文件,成功 返回0 ...... 进程函数,所在函数库为stdlib.h、process.h void abort() 此函数通过调用具有出口代码3 的_exit 写一个终止信息于stderr,并异常终止程序。无返回值 int exec…装入和运行其它程序 ...... 转换子程序,函数库为math.h、stdlib.h、ctype.h、float.h char *ecvt(double value,int ndigit,int *decpt,int *sign)将浮点数value 转换成字符串并返回该字符串 char *fcvt(double value,int ndigit,int *decpt,int *sign)将浮点数value 转换成字符串并返回该字符串 ...... 诊断函数,所在函数库为assert.h、math.h void assert(int test) 一个扩展成if 语句那样的宏,如果test 测试失败,就显示一个信息并异常终止程序,无返回值 void perror(char *string) 本函数将显示最近一次的错误信息,格式如下:字符串string:错误信息 ...... 输入输出子程序,函数库为io.h、conio.h、stat.h、dos.h、stdio.h、signal.h int kbhit() 本函数返回最近所敲的按键 int fgetchar() 从控制台(键盘)读一个字符,显示在屏幕上 ...... 接口子程序,所在函数库为:dos.h、bios.h unsigned sleep(unsigned seconds)暂停seconds 微秒(百分之一秒) int unlink(char *filename)删除文件filename unsigned FP_OFF(void far *farptr)本函数用来取远指针farptr 的偏移量 ...... 存贮分配子程序,所在函数库为dos.h、alloc.h、malloc.h、stdlib.h、process.h int allocmem(unsigned size,unsigned *seg)利用DOS 分配空闲的内存,size 为分配内存大小,seg 为分配后的内存指针 int freemem(unsigned seg)释放先前由allocmem 分配的内存,seg 为指定的内存指针 ...... 操作函数,所在函数库为string.h、mem.h mem…操作存贮数组 ...... ......

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧