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

This is a discussion on replace the "*" in the above string with " ", a single space character using ANSI C?? within the C Programming forums, part of the General Programming Boards category; iostream.h is a deprecated C++ header file. conio.h is not a very portable library. If you can avoid it, and ...

  1. #16
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    iostream.h is a deprecated C++ header file. conio.h is not a very portable library. If you can avoid it, and the getch() function, I would. Oh, and main() returns an int.

  2. #17
    Registered User
    Join Date
    Nov 2006
    Posts
    39

    Talking Thankz For Given Information

    Ya i known it.
    but i worte my program for bigginer, not for Pro.

  3. #18
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    It's not valid C nor is it valid C++, beginner or not.

  4. #19
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You also won't be able to legally nor safely edit foo (it's a string literal)

    char * foo = " Baltimore *MD*is*a*really*good*place*to*work*and*play";
    foo is read-only as far as you're concerned. See MacGyver's post Function with char *

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char s[] = "Baltimore *MD*is*a*really*good*place*to*work*and*play";
        /* Perhaps a loop could go here? That goes from 0 to strlen(s), or 0 to '\0' ? */
        return 0;
    }
    That's as about as much help as you're getting for the solution, if you have specific problems - ask.
    Last edited by zacs7; 08-22-2007 at 06:32 AM.

  5. #20
    Registered User
    Join Date
    Aug 2007
    Posts
    14

    Red face how do i put code in reply?

    i was trying to post my code so i can get further help, i couldnt figure how to do that, help plz?

  6. #21
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I imagine you might be having trouble using code tags.....

    This:

    [code]x = 1337;[/code]

    Produces:

    Code:
    x = 1337

  7. #22
    Registered User
    Join Date
    Aug 2007
    Posts
    14

    Question

    insert
    Code:
    #include <string.h>
    
    int main (void)
    {
         char *foo = " Charlotte *NC*is*a*really*cool*place*to*work*and*play";
         char delims[]="*";
         char *result = NULL;
         result = strtok ( str, delims );
         while ( result != NULL ) 
         {
               printf ("result is "\&#37;s"\n", result );
               result = strtok (NULL, delims );
               }
               return 0;
               }

    ok so now do i get some help, i wrote code, can someone help me correct it?
    yeah macgyver thats exactly what was going wrong when i was trying to post my code, thanks

  8. #23
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    * Read all the posts in this thread, in order. My last post explains why you can't do that (with foo) - it's a string literal, and strtok modifies the string (which is illegal in this case)

    * strtok doesn't help.

    * Include stdio.h

    What don't you understand? You seem to be ignoring what people have been saying. Start at the begging of foo, go through it in a loop, if the current character is '*' then set it to ' ' until you get to the end of foo (ie '\0'). The lastly print the result.

  9. #24
    Registered User
    Join Date
    Aug 2007
    Posts
    14

    Unhappy

    sorry guys, i am complete newbie so taking very long to understand what exactly you guys saying, well i didnt mean to ignore, trust me i read each reply throughout and tried to understand it, i really appreciate it. right now i am working on writing a code the way you said i should, i will be back with that new code

  10. #25
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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.

  11. #26
    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;
    }
    hey zacs7, is this what you meant me to do? please tell me if there is anything wrong wiht it
    or do you think the way you have done it with strlen is better? as in making code short or long?
    Last edited by hardik; 08-22-2007 at 08:10 AM.

  12. #27
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It's almost correct.

    * Remove foo & bar
    * Try to be explicit with the loop, makes for a better read, ie.
    Code:
    while(*replace != '\0')
    {
        if(*replace == '*')
            *replace = ' ';
    /* and so on */
    If you want to keep the original as well as the modified, just make a buffer the same size as baz and write the contents to that (in the loop, char-by-char replacing * with space as you go) just don't forget the null terminator. Otherwise you're done, welldone

  13. #28
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    The easiest solution ever.
    Code:
    #include <stdio.h>
    char text[]={"Charlotte *NC*is*a*really*cool*place*to*work*and*play"};
    
    int main (int argc, char* argv[]){
        int i;
        printf ("&#37;s\n", text);
        for(i=0;text[i];i++){
            if(text[i]=='*'){
                text[i]=' ';
            }
        }
        printf ("%s\n", text);
        return 0;
    }
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  14. #29
    Registered User
    Join Date
    Aug 2007
    Posts
    14
    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.
    Last edited by hardik; 08-22-2007 at 11:59 AM.

  15. #30
    Registered User
    Join Date
    Nov 2006
    Posts
    39

    Angry

    here is rite code .
    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]=' ';
    
    getch();
    }

Page 2 of 4 FirstFirst 1234 LastLast
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, 05:05 AM
  2. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM

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