Thread: my code for string spaces

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    26

    my code for string spaces

    i have this string - "idan damri is here"
    there're many spaces . so i tried to wrote a code that make it normal.
    "idan damri is here "

    its working but not as expected.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <String.h>
    
    
    int main(){
     char string[] ="idan   damri is    here\n";
     char *p = string;
     
     int i = 0;
     int j = 1;
    int size = strlen(string);
    for(; string[i]!= NULL; i++){
    if (string[i] == ' ' && string[j]==' '){
    string[j] ='\b';
    }
    j++;
    }
    printf("%s", string);
    return 0;
    }
    the output is : idan damri ishere
    first two words are fine.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    First, make sure your code is formatted neatly, with consistent indentation:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <String.h>
    
    int main(){
        char string[] ="idan   damri is    here\n";
        char *p = string;
    
        int i = 0;
        int j = 1;
        int size = strlen(string);
    
        for(; string[i]!= NULL; i++){
            if (string[i] == ' ' && string[j]==' '){
                string[j] ='\b';
            }
            j++;
        }
    
        printf("%s", string);
        return 0;
    }


    Second, check your compiler warnings:

    Code:
    /*
    ||=== scrap_c, Debug ===|
    main.c||In function 'main':|
    main.c|13|warning: comparison between pointer and integer|
    main.c|11|warning: unused variable 'size'|
    main.c|7|warning: unused variable 'p'|
    ||=== Build finished: 0 errors, 3 warnings ===|
    */
    The first warning applies to the test case in your "for()" loop. Since you have an array of characters, and not pointers, you shouldn't be checking for NULL. You probably meant to check for the "null character" ('\0'), which is not the same thing.

    However, why not just use "size" since this variable already contains the length of the string?



    Finally, are you sure this logic is how you want to solve the problem? You're basically inserting backspace characters ('\b') into your array for each sequential space after the first. While this might make the string appear to print with single spaces, your array still contains those extra spaces, in addition to '\b' characters. It would be cleaner to implement logic that shifts the characters in your array, rather than adding backspaces.

    However, if you are certain this is how you want to implement the logic, then I suggest you grab a pen and paper and trace through the iterations of the loop to find the problem. I'll offer an illustration to show the issue.

    Code:
    /*
    +---+---+---+---+---+---+---+---+---+---+
    | i | s |   |   |   |   | h | e | r | e |
    +---+---+---+---+---+---+---+---+---+---+
      13  14  15  16  17  18  19  20  21  22
    
    > i = 13, j = 14: do nothing
    > i = 14, j = 15: do nothing
    > i = 15, j = 16: both are spaces, put a '\b' at index 16
    
    +---+---+---+---+---+---+---+---+---+---+
    | i | s |   | \b|   |   | h | e | r | e |
    +---+---+---+---+---+---+---+---+---+---+
      13  14  15  16  17  18  19  20  21  22
    
    > i = 16, j = 17: do nothing
    > i = 17, j = 18: both are spaces, put a '\b' at index 18
    
    +---+---+---+---+---+---+---+---+---+---+
    | i | s |   | \b|   | \b| h | e | r | e |
    +---+---+---+---+---+---+---+---+---+---+
      13  14  15  16  17  18  19  20  21  22
    
    > etc etc...
    */
    The backspace at index 16 eliminates the space at index 15.
    The backspace at index 18 eliminates the space at index 17.
    The result is a loss of all spaces between those words.

    Hopefully this will help you track down the cause of your problem and find a solution.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    157
    Hmm your code doesn't seem to like multiples of 2.
    Last edited by africanwizz; 08-27-2014 at 08:08 AM. Reason: meh..

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    What is your overall approach to solve this problem in words? Are you really wanting to insert backspace characters to "remove" excess spaces? Why not actually remove the excess spaces?

  5. #5
    Registered User
    Join Date
    Aug 2014
    Posts
    26
    Quote Originally Posted by c99tutorial View Post
    What is your overall approach to solve this problem in words? Are you really wanting to insert backspace characters to "remove" excess spaces? Why not actually remove the excess spaces?
    I find a way to just copy tge string to another buffer if its sees double spaces.
    Theres a function for deleteing a character?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,719
    Quote Originally Posted by Idan Damri
    Theres a function for deleteing a character?
    Just overwrite it. You could start with a string like: "aabbcc" in an array of 7 char and end up with a string "abc" in the same array. It might actually look like "abc\0cc", but the last two "cc" that were not overwritten don't matter since they come after the new placement of the null character.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 12-04-2010, 12:04 AM
  2. Replies: 7
    Last Post: 10-03-2009, 10:58 PM
  3. Can a String Have Spaces In It?
    By Grantyt3 in forum C++ Programming
    Replies: 6
    Last Post: 08-28-2005, 09:49 AM
  4. Getting rid of spaces in a string
    By David101 in forum C Programming
    Replies: 38
    Last Post: 09-23-2004, 09:01 PM
  5. Getting rid of spaces in a string
    By scythe in forum C Programming
    Replies: 3
    Last Post: 10-23-2003, 04:05 AM