Thread: replace the "*" in the above string with " ", a single space character using ANSI C??

  1. #31
    Registered User
    Join Date
    Nov 2006
    Posts
    39
    sorry this one is rite.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char * foo = " Baltimore *MD*is*a*really*good*place*to*work*and*play";
    
    int main(void){
    
    for (int i=0 ; i<strlen(foo) ; i++)
        if (foo[i]=='*')
            foo[i]=' ';
    
    return 0;
    }

  2. #32
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    try running it.

  3. #33
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Lol, what's wrong with mine?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  4. #34
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by maxorator View Post
    Lol, what's wrong with mine?
    Nothing, it's better than obaid's in the sense that using strlen as part of a for-loop end-check is a bad thing - especially when the string is being modified inside the loop, as the compiler may not realize if that the length is constant, and thus call strlen EVERY single iteration of the loop.

    --
    Mats

  5. #35
    Registered User
    Join Date
    Jul 2006
    Posts
    10
    obaid's code is modifying a string literal too which are constant.

    Why are there no standard restrictions on modifying string literals?

  6. #36
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Smiley10 View Post
    obaid's code is modifying a string literal too which are constant.

    Why are there no standard restrictions on modifying string literals?
    In trivial cases, it would be possible for the compiler to detect it, but in anything but trivial cases (e.g. when a function takes a char * as a parameter, and that char * happens to be a pointer to a literal).

    It's not just obaid's code that does so, by the way, several of the other examples do too. Maxorators doesn't - but it also doesn't match the original poster's question - and by the way, we haven't seen any post from the OP for a while.... Perhaps the lesson ended? :-)

    --
    Mats

  7. #37
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by hardik View Post
    i have a question for maxotator and zacs7, maxotator, in your code wont we need secondary buffer? same for zacs7 if i remove foo and bar wont i need a secondary buffer? i tried and got an error, also in my code i used strdup because strcpy strcpy will not allocate the memory for the copy for you, can i replace it with something more simple? how is the memory allocated in your code maxorator? oh btw i am asking to understand it not to critisize it, thank you both ofyou.
    The string is stored on the stack (as an array of characters), we step through the original string, changing each * for a space, therefore modifying the original string - meaning no 2nd buffer is required, but if you wanted to... you can store the modified string in another buffer don't copy it at the start, do it as you check for '*' (since it's redundant - you'll be looping twice without knowing it).

  8. #38
    Registered User
    Join Date
    Jul 2006
    Posts
    10
    Quote Originally Posted by matsp View Post
    In trivial cases, it would be possible for the compiler to detect it, but in anything but trivial cases (e.g. when a function takes a char * as a parameter, and that char * happens to be a pointer to a literal).

    It's not just obaid's code that does so, by the way, several of the other examples do too. Maxorators doesn't - but it also doesn't match the original poster's question - and by the way, we haven't seen any post from the OP for a while.... Perhaps the lesson ended? :-)

    --
    Mats
    Thank you.

  9. #39
    Registered User
    Join Date
    Aug 2007
    Posts
    14
    OP is back hehe, well my problem was solved, so BIG THANKS TO ALL YOU GUYS OUT THERE HELPING OTHERS SO MUCH, but also just for sake of learning i am still keeping up with all your posting its interesting how quick one can learn frm a forum like this rather than going thru some biggg lessons and still geting clueless. btw i found maxorator's code most simple and short, but hey smiley i would love if you can explain more throughout that in which case it would not work?
    Last edited by hardik; 08-22-2007 at 07:16 PM.

  10. #40
    Registered User
    Join Date
    Aug 2007
    Posts
    14
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (int argc, char* argv[])
    {
        
        char *foo = "Charlotte *NC*is*a*really*cool*place*to*work*and*play";
        char *bar = strdup (foo);
        char baz [] = "Charlotte
    *NC*is*a*really*cool*place*to*work*and*play";
        char *replace = baz;
        printf ("&#37;s\n", baz);
        while (*replace)
        {
            if (*replace == '*') *replace = ' ';
            replace++;
        }
    
        printf ("%s\n", baz);
    	return 0;
    }
    well maxotator, your code gives correct result but it changes the requirement, i think i would have to use the secondary buffer no matter what, as zacs said i am trying to remove foo and bar, and use char buf[100] as a secondary buffer, i will post back if i succeed, if anyone else can plz let me know

  11. #41
    Registered User
    Join Date
    Aug 2007
    Posts
    14

    Question

    Okay this is as close as you'll get...

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char foo[] = " Charlotte *NC*is*a*really*cool*place*to*work*and*play";
        size_t  i = 0,
                len = strlen(foo);
        for(i = 0; i < len; i++)
        {
             /* foo[i] contains the current character, what should I do with it!?!? */
        }
    
        /* I should probably print foo here... */
        return 0;
    }
    Now it's up to you to complete it and trim it down, it can be written in less code (ie with less variables and less statements... I've merely shown you how to step through foo char-by-char nothing more, nothing less.




    well wont i have to store foo in secondary buffer first? since i cant move/edit original one? what else can i use besides strdup to ease the process?

  12. #42
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You can edit it if it's an array and not a pointer to the string literal. Because it's an array in this last example, you're fine in altering it. strdup()...... mmmm..... no.

  13. #43
    Registered User
    Join Date
    Aug 2007
    Posts
    14

    Unhappy

    Quote Originally Posted by obaid View Post
    sorry this one is rite.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char * foo = " Baltimore *MD*is*a*really*good*place*to*work*and*play";
    
    int main(void){
    
    for (int i=0 ; i<strlen(foo) ; i++)
        if (foo[i]=='*')
            foo[i]=' ';
    
    return 0;
    }
    i tried running it, unsuccessful attempt tho

  14. #44
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    i tried running it, unsuccessful attempt tho
    Just ignore obaid for now. As everyone has been saying, he's trying to modify a string-literal, which is naughty and illegal, he also recalculates the string's size on every iteration - which is slow, as matsp said.

  15. #45
    Registered User
    Join Date
    Aug 2007
    Posts
    14

    Smile

    Code:
    #include<stdio.h>
    #include<string.h>
    int main()
    {
     char *foo = "Charlotte*NC*is*a*really*cool*place*to*work*and*play"; 
     char buf[100];
     int l = strlen(foo);
    int i;
    
     for(i=0;i<l;i++)
     {
      
            if(*foo=='*')
       buf[i] = ' ';
      else
       buf[i] = *foo;
            *foo++;
      
     }
     for(i=0;i<l;i++)
      printf("%c",buf[i]);
      printf("\n");
      getchar ();
    }
    check it out guys, how u like it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM