如何使我的最后3个for循环工作? [英] How do I get my last 3 for loops to work?

查看:69
本文介绍了如何使我的最后3个for循环工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是我的程序的最后三个for循环不运行。该程序应该计算输入中字符的出现次数,显示字符出现的次数以及出现所述次数的字符



The problem is that the last three for loops of my program dont run. The program is supposed to count the occurence of a characters from a input, display the number of times the characters occur and the characters that occur the said number of times

#include<stdio.h>
#include<ctype.h>

int main(void){
    
    char str[50000];
    int x;
    int lowerCase;
    int upperCase;
    int digit;
    int specialCharacter;
    int total;
    int max = 0;
    
    printf("Enter a string and press Control-D when done: \n ");
    scanf(" %c", str);
    
    x = lowerCase = upperCase = digit = specialCharacter = total =0;
    
    for(;str[x]!='\0';x++){
        while ((str[x]= fgetc(stdin)) != EOF){
            
            if(str[x] >= 'A' && str[x] <='Z') upperCase++;
            else if(str[x] >= 'a' && str[x] <='z') lowerCase++;
            else if(str[x] >= '0' && str[x] <='9') digit++;
            else if(str[x] == '\n' || str[x] == '\t' || str[x] == ' '){}
            else specialCharacter++;
            
        }
    }
    
    total = lowerCase + upperCase + digit + specialCharacter;
    
    
    printf("\n%dcharacters\n",total);
    printf("lowercase= %d , uppercase=%d , digit=%d , special=%d \n", lowerCase,upperCase,digit,specialCharacter );
    
    for(int a=0; a<128; a++){
        if(str[a]>max)
            max=str[a];
    }
    
    for(int a=max; a>=0; a--){
        for(int b=0; b<128; b++){
            if(str[b]==a){
                printf("\n\nCharacters occuring %d times: ", str[b]);
                for(int c=33; c<127; c++){
                    if(str[c]==a){
                        printf("%c",c);
                        
                        
                    }
                }
                printf("\n");
                break;
                
                
            }
        }
    }
    
    
}
<pre><pre lang="c#">





我尝试过:



香港专业教育学院我试过改变我的公式,但我什么都没有,我知道循环工作,但它没有显示整个输出。



这是我的输出即时通讯现在开始:







输入字符串并在完成后按Control-D:

你好

4个字符

小写= 4,大写= 0,数字= 0,特殊= 0





出现0次的字符:!#$%&'()* +, - 。/ 0123456789 :;< =>?@ ABCDEFGHIJKLMNOPQRSTUVWXYZ [\] ^ _` abcdefghijklmnopqrstuvwxyz {|}〜



它应该输出到:







输入字符串并在完成后按Control-D:

hello

4字符

小写= 4,大写= 0 ,digit = 0,special = 0



出现2次的人物:l

出现1次的人物:h,e,o

出现0次的字符:!#$%&'()* +, - 。/ 0123456789 :;< =>?@ ABCDEFGHIJKLMNOPQRSTUVWXYZ [\] ^ _` abcdfgijkmnpqrstuvwxyz {|}〜



What I have tried:

Ive tried changing my formulas but ive got nothing, I know the loops work but it doesnt show the entire output.

This is my output that im getting now:



Enter a string and press Control-D when done:
hello
4characters
lowercase= 4 , uppercase=0 , digit=0 , special=0


Characters occuring 0 times: !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

it should output to this:



Enter a string and press Control-D when done:
hello
4characters
lowercase= 4 , uppercase=0 , digit=0 , special=0

Characters occuring 2 times: l
Characters occuring 1 times: h, e, o
Characters occuring 0 times: !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdfgijkmnpqrstuvwxyz{|}~

推荐答案

%&'()* +, - 。/ 0123456789:;< =>?@ ABCDEFGHIJKLMNOPQRSTUVWXYZ [\] ^ _` abcdefghijklmnopqrstuvwxyz {| }〜



它应输出到:







输入一个字符串并在完成后按Control-D:

hello

4个字符

lowercase = 4,uppercase = 0 ,digit = 0,special = 0



出现2次的人物:l

出现1次的人物:h,e,o

出现0次的字符:!#
%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

it should output to this:



Enter a string and press Control-D when done:
hello
4characters
lowercase= 4 , uppercase=0 , digit=0 , special=0

Characters occuring 2 times: l
Characters occuring 1 times: h, e, o
Characters occuring 0 times: !"#


%&'()* +, - 。/ 0123456789 :;< =>?@ ABCDEFGHIJKLMNOPQRSTUVWXYZ [\\ \\] ^ _`abcdfgijkmnpqrstuvwxyz {|}〜
%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdfgijkmnpqrstuvwxyz{|}~


引用:

我知道循环有效,但它不显示整个输出。

I know the loops work but it doesnt show the entire output.



当你不明白你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于如何使我的最后3个for循环工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆