C programming - issues with printing string with backspace characters stored

陌路散爱 提交于 2021-02-10 16:55:34

问题


I am writing a basic C program which takes a word from the user as input and prints that word twice on the same row. The issue that I am facing in printing the word twice is mentioned after the code that I have written below to do this job

void print_word()
{
    char ch; 
    char str[15];
    int i = 0;                /* i will be used as index to access the elements of str */

    printf ("\n Enter a word of your choice : ") ;

    ch = getch() ;         /* Dont echo character */

    while ( !isspace(ch) && i < 14)     /* while loop executes till user doesn't input a space or until the str array is full */
                                        /* isspace() is defined in header file <ctype.h> */
    {
        putchar(ch);           /* Now echo character */
        str[i] = ch ;
        i++ ;
        ch = getch();
    }    //while loop ends

    str[i] = '\0' ;

    printf("\t") ;          //print a gap
    printf ("%s ", str) ;
    printf ("%s", str) ;
}

This function works properly if user directly enters a word (without using backspace to edit the word).

Suppose user enters 'HELLO' then 5 characters viz.'H' 'E' 'L' 'L' 'O' gets stored in array str. But then user presses backspace three times and the word on the console appears 'HE' while str now contains eight characters H-E-L-L-O and 3 backspace characters. But when the printf function in the last two statements of this code is executed and prints str, the first statement correctly prints the 8 character and displays 'HE' on the console, but second statement prints 'HELLO', leaving the 3 backspace characters that are also there in the array str.

Why the last printf() statement is not printing the string str properly i.e., why is it not printing the backspace characters?

The problem remains even if I print str with puts() function or even if the str is printed with a for loop -- character by character. And I want to know what is actually happening in the backend process?


回答1:


Use printf("%s\n", str); both times and you'll see that the first printf does not print 'HE'. It prints 'HELLO' just like the second one, just after that it moves the cursor back 3 times, so if you print both strings on the same line then the second string overwrites the last 3 characters of the first 'HELLO'. After printing HELLO and 3 backspaces you have output HELLO and cursor at the mark:

HELLO
  ^

After printing the space you have:

HE LO
   ^

After printing another HELLO and 3 backspaces:

HE HELLO
     ^

You might want to check this question if you want more information about printing strings with a backspace: Function printf() to print backspace problem.




回答2:


As an alternative to printf("%s\n",str); solution you could process char by char your user's input.

Usually getch() is used whenever the programs require a full control of the input characters, so take advantage of it.

In your while loop just do this, instead:

while ( !isspace(ch) && i < 14)
{
    putchar(ch);           /* Now echo character */
    if( ch == 0x08 && i > 0)
    {
        i--;
    }
    else
    {
        str[i] = ch ;
        i++ ;
    }
    ch = getch();
} 

Basically you need to check if the current input character is a backspace (ASCII value 0x08). If it is, decrease the current index of the input array in order to perform the character deletion. Of course don't do it if the current index is 0.

This approach is common in applications such as AT parsers.




回答3:


This function works properly if user directly enters a word (without using backspace to edit the word).

See this,

 #include<stdio.h>
 #include<conio.h>

 void print_word()
 {
    char ch;
    char str[15];
    int i = 0,flag=0;

    printf ("\n Enter a word of your choice : ") ;

    for(i=0; i<15&&flag==0; ++i)
    {
    ch = getch();
    switch(ch)
    {
      case 13:
         str[i] = '\0';
         flag=1;
         break;
      case '\b':
         if(i>0) i--;
         str[i--]='\0';
         printf("\b \b");
         break;
      default:
         str[i] = ch;
         printf("%c",ch);
    }
    }
    str[15]='\0';

    printf("\t") ;
    printf ("%s ", str) ;
    printf ("%s", str) ;

}

int main()
{
    print_word();
}

You are responsible for removing backspace from str.

Why the last printf() statement is not printing the string str properly i.e., why is it not printing the backspace characters??

your input: HELLO+(3 backspace)

so the str contains {'H','E','L','L','O','\b','\b','\b'}

and the intersection point in console is

  "HE^LLO"

After one tab (nearly 7 spaces), so console appears

  "HE       "

after one print of str

  "HE       HE^LLO"

after print of space and str

  "HE       HE HELLO"


来源:https://stackoverflow.com/questions/60690795/c-programming-issues-with-printing-string-with-backspace-characters-stored

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!