简体   繁体   English

打印字符 C 的 function

[英]A function that prints characters C

My functions change various aspects of a given text.我的函数改变了给定文本的各个方面。 For the following text:对于以下文本:

I am young. You are young.All of us are young.
"I think we need some help.Please" HELP. NO,   NO  NO, 
I DO NOT 
    NEED HELP
WHATSOEVER.
"Today's date is
            15/02/2021"...
I am 18 years old. are you 20 years old? Maybe   30 years?

I need it to be prints like this:我需要它是这样的打印:

I am young. You are young.All of us are young.
"I think we need some help.Please" HELP. NO,   NO  NO, 
I DO NOT 
    NEED HELP
WHATSOEVER.
"Today's date is
            15/02/2021"...
I am  years old. are you  years old? Maybe  years?

For some reason, I have a problem with deleting the numbers and keep in the same state I was prevoisly.出于某种原因,我在删除数字时遇到问题,并保留我之前的 state。 this is my code:这是我的代码:

main(){
    int state,c,cnt,state1;
    cnt=0;
    state = OUT;
    state1=COUNTINUE;
    printf("PLease insert a sentence\n");
    
    while((c=getchar())!=EOF){
        if(isDigit(c)==TRUE) {
            printf("\b");
        }
    
        if (c=='.')
        {
            putchar(c);
            state =OUT;
        }
        if (c=='"')
        {
            state = IN;
            cnt+=1;
        }
        if(cnt==1)
            state =IN;
    
        if(cnt==2)
        {
            cnt=0;
            putchar(c);
            state = OUT;
        }
    
        if(state == OUT &&isChar(c)==TRUE ){
            if(isUpper(c) ==TRUE)
                putchar(c);
            else
                putchar(toUpper(c));
        
            state = COUNTINUE;
        }
        else if(state==COUNTINUE)
            if(isUpper(c)==FALSE)
                putchar(c);
            else
                putchar(toLower(c));
        else if(state ==IN &&c!='.' &&isDigit(c)==FALSE)
            if(isUpper(c)==FALSE)
                putchar(toUpper(c));
            else
                putchar(c);
    }
}

This is the output Im given:这是我给出的 output:

I am young.You are young.All of us are young."I THINK WE NEED SOME HELP.PLEASE"Help.No,   no  no, 
i do not 
    need help
whatsoever."TODAY'S DATE IS
          "...I am8 years old.Are you0 years old? maybe  0 years?

Looking at the input and required output, I don't understand why would you use toUpper() and toLower() functions.查看输入和所需的 output,我不明白您为什么要使用toUpper()toLower()函数。 Anyway, your approach is wrong.无论如何,你的方法是错误的。 The below code is enough to skip digits.下面的代码足以跳过数字。

while((c=getchar())!=EOF){
    if(isDigit(c)==true){
       continue;
    }
    putchar(c);
}

Now, I assume you want to print digits in "" ie double quotes.现在,我假设您想在""中打印数字,即双引号。 In required output it's 15/02/2021 .在所需的 output 中,它是15/02/2021 For that, you would need to use a flag .为此,您需要使用flag Set flag=1 if you encounter c='"' which indicates we are inside the "" ie double quotes. If we encounter another c= '"' which indicates end of double quote pair, we set flag=0 .如果遇到c='"'设置flag=1 ,表示我们在""即双引号内。如果遇到另一个c= '"'表示双引号对结束,我们设置flag=0 The below code prints digits when inside "" .下面的代码在""内打印数字。

flag=0;
while((c=getchar())!=EOF){
        if(c=='"'){
            flag=!flag;//swapping 0 and 1
        }
            if(isDigit(c)==true && flag==0){
               continue;
            }
            putchar(c);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM