Thread: Replace # into backspace

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    21

    Replace # into backspace

    Hi im writing program from codewars site.
    The aim of this program is to replace # into \b. After compiling in Visual Studio I am getting proper result but codewars site give me wrong result.
    Code:
    char *strclr(const char *s)
    {
    	char *r = (char*)malloc(sizeof(char)*strlen(s) + 1);
    	memset(r, '\0', strlen(s) + 1);
    	for (; *s!='\0'; s++)
    	{
    		if (*s == '#')
    			strncat(r, "\b", 1);
    		else
    			strncat(r, s, 1);
    	}
    	return r;
    }
    I think that I could make one change in this program if *s=='#' decrease --r but it not work properly. Can you help me with this

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    Is your aim actually to replace each '#' with '\b', or is it to remove each '#' from the string?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    21
    Remove # and remove previous char for expample "abc#d##c" should give me "ac"

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,994
    Do you want the actual string "ac", or just something that prints as "ac" because it's got backspace characters in it.

    Such as "abc\bd\b\bc"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2019
    Posts
    21
    Probably actual string should be "ac" because my solution give me string with backspaces and codewars compiler give me not proper solution

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,748
    In that case, I would suggest treating an array of char as a stack: whenever you encounter a character that is not a '#', push it into the stack. When you encounter a '#', pop from the stack if the stack is not empty (and discard the '#', of course). After you have processed the entire input string, treat the resulting stack as a string (and indeed the last character to be pushed into the stack should be a '\0'), and you're done.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,994
    So do you get any actual useful information out of codewars - you know, more than "it doesn't work"?

    Also, I'm guessing "abc#d##c" isn't the actual (or only) string being tested.
    You need to take care of all the pathological cases such as "#####" and "ab###cd" that would underflow the string if treated naively.

    Not that it hasn't happened before.
    FGA: The CSRSS Backspace Bug in Windows NT 4/NT 2000/NT XP
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    3,824
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. backspace
    By Tool in forum C Programming
    Replies: 1
    Last Post: 09-06-2009, 01:07 PM
  2. Printing a backspace
    By Leima in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 05:56 PM
  3. Backspace in terminal
    By IceDane in forum C Programming
    Replies: 1
    Last Post: 10-14-2007, 05:53 PM
  4. Backspace
    By Necrofear in forum C++ Programming
    Replies: 13
    Last Post: 05-11-2006, 01:16 PM
  5. backspace info
    By kermit in forum Linux Programming
    Replies: 4
    Last Post: 11-28-2003, 02:55 PM

Tags for this Thread