Problem with string output...

This is a discussion on Problem with string output... within the C Programming forums, part of the General Programming Boards category; Ok people.. this is getting aggravated, and, even though I'm sure it's something very stupid, I can't figure out why ...

  1. #1
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60

    Angry Problem with string output...

    Ok people.. this is getting aggravated, and, even though I'm sure it's something very stupid, I can't figure out why this string isn't getting printed.
    The little piece of code is supposed to go through a string and remove the '/' character as well as the one right before. So if I have: "Free/d" as my sting, I'm supposed to end up with "Fred". Pretty Simple right?

    Here's code (it's not much, I just wanted to make sure my idea was working)
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main () {
    
     	char string[30];
    	int i, j;
    	char temp[30];
    
    	strcpy (string, "Blee/ssee/d is tt/he man/.\n");
    	printf("%s\n", string);
    
    	for (i = j = 29; i >= 0; ) {
    		if (string[i] != '/') {
    			temp[j] = string[i];
    			printf("temp = %c, string = %c\n", temp[j], string[i]);
    			i--;
    			j--;
    		}
    		else
    			i = i-2;
    	}
    
    	printf("%s\n", string);
    	printf("%s\n", temp);
    
    return;
    }
    My problem is when I run that, string is correct, but temp is blank, even though the printf statement in the for loop makes it print the correct stuff!!
    Can anyone explain that to me?

    I just tried something else, and temp prints by characters, but won't prints by string.
    ie: %c works, %s doesn't!
    Last edited by Lau; 11-17-2003 at 01:06 PM.
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you doing it the hard way? Go from the front of the string to the end. If you don't, you'll end up with a misaligned string. Consider:
    Code:
    s1 = "hi/!"
    s2 = where we put stuff
    
    s2[3] = s1[3] (the '!')
    s2[2] = s1[1] (the 'i")
    s2[1] = s1[0] (the 'h')
    s2[0] = oops, no more s1, so what does it have?
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    Quzah,

    But if I go from the front to the back, I will have "processed" the character that I want to delete.
    The '/' is supposed to mean that both itself, and the preceding character will be deleted, not just itself:
    ca/at becomes cat

    However, that's not really my concern about this piece of code. I don't understand why neither string will print when I call printf!!!!
    Last edited by Lau; 11-17-2003 at 01:52 PM.
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    Mmmmmm
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char string[30];
        int i, j;
    
        strcpy(string, "Blee/ssee/d is tt/he man/.\n");
        printf("%s\n", string);
        for ( i = 0, j = 0 ; string[i] ; i++ ) {
            if ( string[i] == '/' ) j--;
            else string[j++] = string[i];
        }
        string[j] = '\0';
        printf("%s\n", string);
        return 0;
    }
    Since you know the string can never get any longer, an in-place change is easiest to do
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    Thank you Salem!!
    I actually works... )

    So, my porblem is fixed.
    But I still don't understand why the printf gave me a blank line before (??)
    -----------
    I found a littel flaw in this code Salem. If anything is in fact deleted from the string, the end of it remains. I couldn't understand why I had an ouput like that:

    Blessed is the man.
    man.

    I just added
    Code:
    while (string[j]) {
    	string[j] = ' ';
    	j++;
    }
    after what you had written
    Last edited by Lau; 11-17-2003 at 03:59 PM.
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by Lau
    But I still don't understand why the printf gave me a blank line before (??)
    It's because you started at the end of the string, loading the temp buffer from the end. When loaded, the string was shorter than the original leaving you with nulls at the beginning of temp. So temp[0] was equal to '\0' which makes temp an empty string.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Lau
    Lau is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    Go it Walt!!
    Thanks
    "Shallow men believe in luck. Strong men believe in cause and effect."
    -Ralph Waldo Emerson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. string problem
    By INeedSleep in forum C++ Programming
    Replies: 24
    Last Post: 11-08-2007, 10:52 PM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. string pattern search problem
    By goron350 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 07:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21