c语言 gets 指针,关于gets ( )的用法! 利用指针求字符数额

关于gets ( )的用法! 利用指针求字符数目

下面这个程序我断点调试时,停在p=str 哪儿了?为什么啊?

//输入一行文字,找出其中的字符各有多少?

# include 

int main ()

{

int upper=0,lower=0,space=0,digit=0,other=0;

char str[256],*p;

printf ("input string:");

gets(str);

p=str;                    //这里有问题吗?

while (*p!='\0')

{

if ((*p>'A')&&(*p

upper++;

else if ((*p>'a')&&(*p

lower++;

else if (*p=' ')

space++;

else if ((*p>'0')&&(*p

digit++;

else

other++;

}

printf ("upper case :%d\tlower case :%d\t",upper,lower);

printf ("space :%d\tdigit :%d\tother :%d",space,digit,other);

return 0;

}

------解决方案--------------------

你指出的地方没有问题,问题出在while内部没有p++,死循环了

------解决方案--------------------

#include 

int main ()

{

int upper=0,lower=0,space=0,digit=0,other=0;

char str[256] = "\0",*p; //str 最好初始化

printf ("input string:");

gets(str);

p=str;                    //这里有问题吗?

while (*p!='\0')

{

if ((*p>='A')&&(*p<='Z'))//1

upper++;

else if ((*p>='a')&&(*p<='z'))  //2

lower++;

else if (*p==' ')  //少了一个等号  建议再以后判断的时候这样写if(' ' == *p) 这样掉了一个等号编译器会报错

space++;

else if ((*p>='0')&&(*p<='9'))//1 2 3 范围有问题

digit++;

else

other++;

p++; //判断一次之后p要往后移动

}

printf ("upper case :%d\tlower case :%d\t",upper,lower);

printf ("space :%d\tdigit :%d\tother :%d",space,digit,other);

return 0;

}

------解决方案--------------------

//输入一行文字,找出其中的字符各有多少?

# include 

int main ()

{

int upper=0,lower=0,space=0,digit=0,other=0;

char str[256],*p;

printf ("input string:");

gets(str);

p=str;                    //这里有问题吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值