TechECE

Ramblings of a directionless group...

Contributors
Our Personal Blogs
Subscribe to our feed

Enter your email address:

Delivered by FeedBurner

Wednesday, April 05, 2006
Teaser!
I was facing a problem in a "single line text editor" kind of code. It is "kind of" but not the actual editor since select copy paste and all the weird thins are not there. A simple section of pseudo code is as:

do
{
if (key == backspace)
{
string[index]='\0';
index--;
}
else if(isprint(key))
{
string[index]=key;
index++;
}
printf("\r%s",string);
}while(key = getkey() != Enterkey);

getkey is more like getch.

As you can see if I give backspace the change will not be reflected on the screen. How do I refelect it?

The fix is not a great deal. I took sometime to settle on the soultion.

PS: Assume support of conio is not there.
posted by Sunil @ 5:37 AM  
3 Comments:
  • At 11:20 PM, Blogger Sunil said…

    Well pradeep, I don't expect the exact answer as I have in my mind. Anything better is also fine. Your idea is close to mine, but won't work :)

    If i enter a sequence

    abcd[backspace][backspace]123

    string will be

    abcd123

    Sundaravana, if you mean adding \n in place of \r? But it will screw the display right?

     
  • At 4:29 AM, Blogger Sunil said…

    if (backspace)
    {
    string[index--] = 'space';
    string[index] = '\0';
    }

    Initial String:
    abc(null)=>[index=3]

    If you enter backspace,
    abc(backspace)=>string[3]=' ' and string[2]='\0'

    ab(null)(space)->this will be the new string

    This won't work again.

    I will give a clue (or constraint), you could add/remove/modify exactly 3 characters :)
    At any point the variable string should not contain stray spaces. Only the user entered character should go there.

     
  • At 9:57 PM, Blogger Sunil said…

    Bingo!!!
    That is what I had in my mind da. I already told your idea of adding space at the end is closer to mine.

     
Post a Comment
<< Home
 
Previous Post
Blogroll
Archives
Personal Blogs by Indian Bloggers