主题:[讨论]大侠们帮我看下这段while循环哪里出问题了,多谢。
			
 全频阻塞干扰
				 [专家分:0]  发布于 2011-03-30 23:46:00							
			高手们帮我看下这段代码,我是先把输入的字符串里的标点符号变成空格,然后计算空格的数量,但是运行不成功,运行后只能输出替换后的字符串,然后按任意键都没有反应了,不知道是while循环哪里出问题了。
[color=FF0000]int main(void)
{
    char text[LENGTH];
    int i=0;
    int j=0;
    printf("\nEnter the paragraph(less than 10000 characters):\n");
    gets(text);
    while(text[i]!='\0')
        ispunct(text[i])?text[i++]=' ':i++;
    printf("\n%s\n",text);
    
    i=0;
    while(text[i]!='\0');
    {
        if(int (text[i++])==32)
           j++;
        else
            i++;
    }
    printf("%d",j);
    return 0;
}[/color]
			最后更新于:2011-03-30 23:46:00
			
					 
		
			
回复列表 (共4个回复)
		
								
				沙发
				
					
aiby [专家分:15340]  发布于 2011-03-31 10:40:00				
				while(text[i]!='\0');
							 
						
				板凳
				
					
hnuhsg1226 [专家分:150]  发布于 2011-03-31 12:58:00				
				#include<stdio.h>
#include<ctype.h>
#include<string.h>
#define LENGTH 1000
int main()
{
    char text[LENGTH];
    memset(text,'\0',sizeof(text));
    int i=0;
    int j=0;
    printf("\nEnter the paragraph(less than 10000 characters):\n");
    gets(text);
    while(text[i]!='\0')
        ispunct(text[i])?((text[i]=' ')&&j++):i++;
    printf("\n%s  %d\n",text,j);
    return 0;
}
							 
						
				3 楼
				
					
全频阻塞干扰 [专家分:0]  发布于 2011-03-31 18:40:00				
				[quote]while(text[i]!='\0');[/quote]
谢谢了,老犯这种小错误
还有问下如果我想从键盘输入一篇文章,有好几个段落,该怎么用gets,因为这些函数都是只要输入回车就停止输入,只能输入一段。
							 
						
				4 楼
				
					
diycai [专家分:1590]  发布于 2011-03-31 21:32:00				
				#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#define MaxLen 10000
void main()
{
    char text[MaxLen],c;
    int i = 0, j = 0;
    
    printf("Enter the paragraph(less than %d characters):\n",MaxLen);
    
    while(1)
    {
        c = getche();
        if(c == 27)        //按下ESC键退出输入数据
        {
            text[i] = '\0';
            break;
        }
        if(i == MaxLen-1)
        {
            printf("\n\n\nWarning:Overflow!\n");
            text[i] = '\0';
            break;
        }
        if(c == 13)
        {
            c = '\n';
            printf("\n");
            text[i++] = c;
            continue;
        }
        if(c == ' ')
        {
            j++;
        }
        if(ispunct(c))
        {
            c = ' ';
            j++;
        }
        text[i++] = c;
    }
    printf("\n%s\n",text);
    printf("Length=%d\tSpace=%d\n",i,j);
    while(1)    
    {
        getch();
    }
}
							 
									
			
我来回复