Thread: Need some help with strings

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    34

    Need some help with strings

    My Professor said I should look over a couple problems in the book so that I could gain some understanding of strings. We have just briefly touched on them, and we don't have to write any program using them, but I thought I would post these programs here to see if anyone would be willing to explain how to do them. Like I said, these are not "homework" problems, but rather "practice" problems that he said would be good to know (for the final perhaps?). Anyways, here they are if anyone can help me with them:

    Write a function that accepts a string (a point to a character) and deletes the last character by moving the null character one position to the left.

    Write a function that accepts a string (a pointer to a character) and deletes all the trailing spaces at the end of the string.

    I haven't had much time to look over the chapter, and as I said we only touched on strings in class, so I'm not too educated about them yet.

    TIA for any help.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    void remove_null(char* str)
    {
    	str[strlen(str) - 1] = '\0'; /* Removes last character in array before NULL */
    }
    
    void remove_trailing_spaces(char* str)
    {
    	for (int i = strlen(str) - 1; i >= 0; i--)
    	{
    		if (str[i] == ' ') continue;
    		else /*if (str[i] != ' ')*/
    		{
    			str[i + 1] = '\0';
    			break;
    		}
    	}
    }
    Now tested, but should work theoretically.
    Last edited by Elysia; 11-30-2007 at 02:22 AM.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why not this:
    Code:
    void remove_trailing_spaces(char* str)
    {
    	for (int i = strlen(str) - 1; i <= 0; i--)
    	{
    		if (str[i] != ' ')
    		{
    			str[i + 1] = '\0';
    			break;
    		}
    	}
    }
    Your solution NEEDLESSLY uses continue. [Or did you just do that to tease ME?]
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Actually, I added it to make it more readable and couldn't be bothered to comment it out since I'd have to change the If...ELSE instead. Likewise I commented out the IF after else just to make it more readable.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    34
    So the first program is as simply as just:

    Code:
    void remove_null(char* str)
    {
    	str[strlen(str) - 1] = '\0'; /* Removes last character in array before NULL */
    }
    ?

    So basically this is saying that the string length is supposed to be the string -1, am I interpreting this correctly?

    And for the remove trailing spaces function, it is basically saying string + 1 = 0 trailing spaces? Is this the explanation for the most part?

    Thanks for your help.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For the first: strlen returns the length of the string, not including the NUL char. However, the array is the length of the string + NUL char - minus one. So that means strlen would return the position to the end of the array, so we subtract one from that and set it to the new NUL char and thus truncate the last char.

    For the second example, we walk from the end of the array so long as we find spaces. When we encounter the first non-space, we add one to index and truncate that character with a NUL char, thus truncating the rest of the string beyond that part.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Since strlen() returns a size_t, I would have written the loop in remove_trailing_spaces() like
    Code:
      size_t i; /* this is C90, presumably */
      for (i = strlen(str); i != 0;) { /* I think you meant >=, not <=, by the way */
        --i;
        /* inner code */
      }

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, of course... it returns a size_t, unsigned long if I'm not mistaken. It would probably have used that if I tested the code. I didn't think of that.
    But, no, I did mean <=. So long as i isn't 0 and = to execute the condition if i IS 0 (since 0 is a valid array element). You also add that --i to the loop header too.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    But you want the values of i inside the loop to count _downwards_, starting with strlen(str) - 1 and ending with 0. With <=, the loop never executes if strlen(str) > 1. The reason I put the --i inside the loop is that ">= 0" doesn't work with an unsigned variable, since it's always true. (I could have used > instead of != in my version, though.)

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh, wait, wait... RIGHT! Lol. Bug!
    It should be >=.
    Fixed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM