#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    1

    Character replacement


    This is the code I am trying to use to create a program which replaces each backspace with '\b' and each tab by '\t' in its output:
    Code:
    #include <stdio.h>
    
    main()
    {
    	int c;
    		
    		while ((c = getchar()) != EOF) {
    			if (c == '\b')
    			printf("\b", c);
    			if (c == '\t')
    			printf("\t", c);
    			else
    		    putchar(c); 
    		}
    }
    The program doesn't work as expected. It doesn't replace the characters. Please help me figure out where I am going wrong. Thank you! :)
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    If you want to print a \ in C, remember you have to represent it as "\\" because \ is an escape character.

    Therefore you should call printf("\\b") and printf("\\t") to print the strings correctly.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    1
    Originally Posted by Scorpions4ever
    If you want to print a \ in C, remember you have to represent it as "\\" because \ is an escape character.

    Therefore you should call printf("\\b") and printf("\\t") to print the strings correctly.
    Thanks a lot. The "\\b" and "\\t" worked, but how do I replace a single backslash ("\") from input with "\\" in output?
  6. #4
  7. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3269
    "\\\\". The first and third backslashes escape the second and fourth.
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
    DaWei on Pointers Politically Incorrect.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    1
    Originally Posted by DaWei_M
    "\\\\". The first and third backslashes escape the second and fourth.
    Thanks! It worked like a charm. One last thing about character replacement. How do I replace two or more consecutive blanks with a single blank?
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,593
    Rep Power
    4207
    Originally Posted by arman.khandaker
    How do I replace two or more consecutive blanks with a single blank?
    Detect if a char is a blank and detect if the next char is a blank and so on until you hit a non-blank char. Then write out one space and the non-blank char you hit.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Location
    Dhaka, Bangladesh
    Posts
    116
    Rep Power
    1
    Originally Posted by Scorpions4ever
    Detect if a char is a blank and detect if the next char is a blank and so on until you hit a non-blank char. Then write out one space and the non-blank char you hit.
    That's where my problem lies, how do I check if the NEXT consecutive char is a blank? It would be helpful if you could show me how to modify my original code to do this.
  14. #8
  15. Lord of Dorkness
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2004
    Location
    Central New York. Texan via Arizona, out of his element!
    Posts
    8,524
    Rep Power
    3269
    Once you hit a blank, keep reading until you hit a non-blank. Then output one blank and the non-blank.
    Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
    If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
    DaWei on Pointers Politically Incorrect.

IMN logo majestic logo threadwatch logo seochat tools logo