Thread: Insert a character in a string

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    6

    Insert a character in a string

    Hi!
    Lets say I have a string with numbers "123456789". How can I insert commas in the string to make it look like this "123,456,789"?I've tried to realloc the memory every time i needed to insert a comma and then to put the character from the position n to position n+1 and then to insert the comma but it didn't work i got something like 123,56,89(.
    Can anyone help me please,I have a deadline till Friday?!

    Thanks,
    Marius.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could use the string length to determine how many commas (n) will have to be inserted. Then you can copy the last 3 characters n places to the right, and then insert a comma before them. Then you copy the next three characters (n-1) places to the right, so on and so forth. (You should copy starting from the end in order to avoid overwriting characters prematurely.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    oke i understand,i had the same idea but what function can i use to copy two characters from the third position to the fifth position,cause i don't know any...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could use memmove, but I would just use a loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    i've done this so far:

    for(k=0;k<strlen(alfa.sir);k++)
    if((strlen(alfa.sir)-k)%3==0 && k>0){
    alfa.sir=(char*)realloc(alfa.sir,(strlen(alfa.sir) +1)*sizeof(char));


    and after that i don't know what function to use.
    alfa.sir is my string("1234567)

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    Quote Originally Posted by laserlight View Post
    You could use memmove, but I would just use a loop.
    oke thx i'll try with memmove.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    oke i've tried with memove but i think i've got it wrong...this is what i've done but still it doen't work:

    Code:
    for(k=0;k<strlen(alfa.sir);k++)
            if((strlen(alfa.sir)-k)%3==0 && k>0){
                alfa.sir=(char*)realloc(alfa.sir,(strlen(alfa.sir)+1)*sizeof(char));
                for(j=strlen(alfa.sir)-1;j>k;j--)
                    memmove(alfa.sir[strlen(alfa.sir)],alfa.sir[j],1);
                alfa.sir[j]=',';
                }
    i think the first two arguments of memove have to be pointers,right?can u help and tell me what i'm doing wrong?

    thank you!

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    36
    Try changing to memmove(&alfa.sir[strlen(alfa.sir)],&alfa.sir[j],1);
    Last edited by Annihilate; 02-09-2011 at 01:00 PM.

  9. #9
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    there u go....i totally different approach though

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(){
        char * str = "123454235453425";
        int n_sz = strlen(str), commas;
        if (n_sz % 3 == 0){
            commas = (n_sz / 3) - 1;
        }
        else{
            int mod = n_sz % 3;
            commas = ( n_sz - mod ) / 3;
        }
        n_sz = commas + strlen(str);
        char * new_str = (char *)malloc(n_sz);
        int i = strlen(str) - 1;
        for (; i >= 0; i--){
            if (((strlen(str) - i) % 3 == 0) && i != 0){
                new_str[i + commas] = str[i];
                new_str[i + --commas] = ',';
            }
            else{
                new_str[i + commas] = str[i];
            }
        }
        new_str[n_sz] = '\0';
        printf("%s\n", new_str);
        return 0;
    }

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    6
    Thanks a lot guys it's my first time when I ask help on a forum and you've been very very kind and useful!
    Have a great night!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking to shlwapi.lib in C, MSVC CMD.
    By Joerge in forum Windows Programming
    Replies: 4
    Last Post: 08-07-2009, 05:18 PM
  2. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Replies: 4
    Last Post: 10-14-2005, 12:53 PM
  5. Replies: 3
    Last Post: 11-03-2002, 02:14 AM