Thread: Xstrcpy-replace all characters in a string with single character..

  1. #1
    Registered User thriller500's Avatar
    Join Date
    Oct 2011
    Posts
    25

    Xstrcpy-replace all characters in a string with single character..

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    void xstrchr(char*,char);
    int main()
    {
        char *target;
        char s;
        int l;
        printf("Enter the target\t");
        gets(target);
        printf("%s\n",target);  /*debug*/
        printf("Enter the character\t");
        scanf("%c",&s);
        xstrchr(target,s);
    }
    
    
    
    
    void xstrchr(char *target,char s)
    {
        printf("\n%s\n",target); /*debugging*/
        while(*target!='\0')
    {
        *target=s;
       printf("%s\n",target);
    
    
       *target++;
         }
    
    
    }



    Aim : Replace all characters in a string with single character,

    but the problem is "*target++" .

    i am not able to print the entire replaced string


    i have attached the execution to the post
    Attached Images Attached Images Xstrcpy-replace all characters in a string with single character..-execution-png 

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    There is no memory allocated for your target variable. You're basically using gets (which is bad by itself, you should use fgets) to write into a random part of memory. This is called undefined behaviour and is (to quote laserlight) a Bad Thing.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User thriller500's Avatar
    Join Date
    Oct 2011
    Posts
    25
    Quote Originally Posted by QuantumPete View Post
    There is no memory allocated for your target variable. You're basically using gets (which is bad by itself, you should use fgets) to write into a random part of memory. This is called undefined behaviour and is (to quote laserlight) a Bad Thing.

    extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
    __wur;

    * Get a newline-terminated string from stdin, removing the newline.
    DO NOT USE THIS FUNCTION!! There is no limit on how much it will read.


    This function is a possible cancellation point and therefore not
    marked with __THROW. */


    i have started c programming recently, i am sorry but i didnt get ur answer..
    i tried replacing gets with fgets..

    but what i want to do is a c programs ( so i didnot include a iostream)

    fgets needs more arguments, i was not able to understand what i need to place in " fgets(..) "..


    .................................................. ...............................
    i want to do this program using pointers is because it is space efficient.. i could have done it using arrays..
    but the flexibility of the program will be affected ( size of string )..

    if u could repair my code .. i would be grateful..
    ........................
    Sorry for my bad english..

  4. #4
    Registered User thriller500's Avatar
    Join Date
    Oct 2011
    Posts
    25
    what i want is:
    string : target
    character to replace with : r

    OUTPUT :
    rrrrrr

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You start with
    Code:
    char target[100];
    Then
    Code:
    if ( fgets( target, sizeof(target), stdin ) ) {
      // do your thing here
    }
    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.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by thriller500 View Post
    Aim : Replace all characters in a string with single character,

    but the problem is "*target++" .

    i am not able to print the entire replaced string
    There is another problem here, you are printing inside your loop with %s, that is why you get the current character 's' and the remaining string that is not yet changed. Just remove the printf in the loop and print target in it's entirety after you called your function.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > Replace all characters in a string with single character,
    Code:
    memset(target, s, strlen(target));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-06-2010, 11:45 AM
  2. Replies: 1
    Last Post: 02-05-2010, 02:59 PM
  3. replace single character with matrix of char
    By rob90 in forum C Programming
    Replies: 7
    Last Post: 12-28-2009, 03:19 PM
  4. Replies: 46
    Last Post: 08-24-2007, 04:52 PM