C 编程 - 打印存储退格字符的字符串问题 [英] C programming - issues with printing string with backspace characters stored

查看:124
本文介绍了C 编程 - 打印存储退格字符的字符串问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基本的 C 程序,它将用户的一个单词作为输入并在同一行上打印该单词两次.在我为完成这项工作而编写的代码之后提到了我在两次打印单词时面临的问题

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).

假设用户输入 'HELLO' 然后输入 5 个字符,即'H' 'E' 'L' 'L' 'O' 被存储在数组 str 中.但随后用户按退格键三次,控制台上的单词显示为HE",而 str 现在包含 8 个字符 H-E-L-L-O 和 3 个退格字符.但是当这段代码的最后两条语句中的 printf 函数被执行并打印 str 时,第一条语句正确打印了 8 个字符并在控制台上显示了 'HE',而第二条语句打印了 'HELLO',留下了 3 个退格字符也在数组 str 中.

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.

为什么最后一个 printf() 语句没有正确打印字符串 str,即为什么它没有打印退格字符?

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

即使我用 puts() 函数打印 str ,或者即使 str 用 for 循环打印 -- 一个字符一个字符地打印,问题仍然存在. 我想要想知道后端流程中实际发生了什么?

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?

推荐答案

使用 printf("%s\n", str); 两次,你会看到第一个 printf 没有打印他".它像第二个一样打印HELLO",然后将光标移回 3 次,因此如果在同一行上打印两个字符串,则第二个字符串会覆盖第一个HELLO"的最后 3 个字符.打印 HELLO 和 3 个退格后,您将在标记处输出 HELLO 和光标:

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
  ^

打印空间后:

HE LO
   ^

打印另一个 HELLO 和 3 个退格后:

After printing another HELLO and 3 backspaces:

HE HELLO
     ^

如果您想了解有关使用退格键打印字符串的更多信息,您可能需要检查此问题:函数 printf() 打印退格问题.

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

这篇关于C 编程 - 打印存储退格字符的字符串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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