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; How about a 1 Liner: Code: #include<stdio.h> int main() { char fooBar[] = "Charlotte*NC*is*a*really*cool*place*to*work*and*play"; char *p; for(p=fooBar; *p ; p++ ...

  1. #46
    Registered User
    Join Date
    Aug 2007
    Posts
    42
    How about a 1 Liner:

    Code:
    #include<stdio.h>
    
    int 
    main() {
     char fooBar[] = "Charlotte*NC*is*a*really*cool*place*to*work*and*play"; 
      char *p;
    
       for(p=fooBar; *p ; p++ )  if (*p=='*') *p = ' ';
       
       printf("%s\n",fooBar);
    
    }
    Or
    Code:
         p = fooBar;
         while(p=strchr(p,'*') ) *p++='*';

  2. #47
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by hardik View Post
    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?
    Don't increase the foo pointer, or else you'll lose what it's pointing to. And if you null-terminate buf, you'll be able to print it as a string, ie
    Code:
    printf(buf)

Page 4 of 4 FirstFirst 1234
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

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