为什么\ 0适用于该程序但不适用于在子函数中执行while循环的情况? [关闭]

问题描述 投票:-9回答:1
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
clrscr();
 char x[80];
 int vo,co,d,ws,sp;
 vo=co=d=ws=sp=0;
 void count(char x[],int *vo,int *co,int *d,int *ws,int *sp);
 printf("\n ENteer a astring");
 gets(x);
 count (x,&vo,&co,&d,&ws,&sp);
 printf("\nvowel=%d\ncons=%d\n dig=%d\n white=%d\n specal=%d\n",vo,co,d,ws,sp);
 getch();
}

void count(char x[],int *vo,int *co,int *d,int *ws,int *sp)
{
 char y;
 int i=0;
 do{
    y=toupper(x[i]);
    if (y=='A'||y=='E'||y=='I'||y=='O'||y=='U')
    ++*vo;
    else if (y>='A'&&y<='Z')
     ++*co;
    else if (y>='0'&&y<='9')
     ++*d;
    else if (y==' '||y=='\t')
     ++*ws;
    else
    ++*sp;
    i++;
    } while (x[i]!='\n');
}

P.S程序使用turbo c以c语言编写。用于计算元音,辅音,空格,数字和特殊字符的程序。对不起,如果我没有正确处理这个问题。我是stackoverflow的新手。

OUTPUT when I use \n in while loop

OUTPUT when I use \0 in while loop

c function pointers
1个回答
2
投票
char *gets(char *s);

gets()从stdin读取一行到s指向的缓冲区,直到终止换行符或EOF,它替换为空字节('\ 0')。

我希望你得到你的答案。

PS gets()是一个危险的函数,不应该使用因为函数不知道缓冲区有多大,所以它继续读取,直到找到换行符或遇到EOF,并且可能溢出缓冲区的边界。给定,使用fgets()代替。

© www.soinside.com 2019 - 2024. All rights reserved.