Thread: strings

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    8

    strings

    Hello everyone. I'm looking to expand my knowledge about strings but I am stuck on a particular problem... I am hoping that by writing it down and trying to explain my problem I might be able to think of a solution or get hints on how to solve it.

    I want to be able to search through a string for a particular character and insert a word inside.

    So for example I have the string "dog" and would like to replace the character "o" with the string "cat".

    So the output with be dcatg.
    This is what I have so far
    but it doesn't work

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[]){
    
       char str1[15] = "dog";
       char str2[15] = "cat";
       int i;
       char temp[15];   
    
       for(i=0;i<3;i++){
    
          if(str1[i]="o"){
             
             for(j=i;j<3;j++){
                temp[j]=str1[j];
             }
             
             strcmp(str1[i], str2);
             
             for(j=i;j<3;j++){
                str1[j]=temp[j];
             }
        }
    }
                  
    
    
    return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547

    Thumbs up

    Ok, it's probably a good idea that you get more familiar with the string library (#include string.h) and maybe learn a little more about pointers.

    Basically what you have to do is locate the desired character an o in this case... take a look at what strchr() does for you.
    Once you find it you need to copy out the rest of the string to a temp buffer, which I see you've already made ... look at strcpy() for this
    Then you need to inject your new text overwriting all or some of the tail part... again strcpy() does a good job
    Finally you need to concactenate (add) the tail back onto your string...

    Code:
    void InjectString(char *dst, char key, char *new)
       { char *ptr;               // pointer for string manipulation
          char temp[16];           // temp buffer for injection
          ptr = strchr(dst,key);          // find key character
          strcpy(temp, ptr);            // copy the tail skipping the key
          strcpy(ptr,new);                //  inject the new text at ptr
          strcat(dst,temp); }            // put the tail back.
    You would call this as... InjectString("absolutely",'l',"freaking");
    and you would get back absofreakinglutely...

    This is why there is a standard C library, it makes tasks like this easy. Yes, you can do it with loops, but that's a lot of work... unless you are deliberately trying to learn about arrays and loops.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  2. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  3. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  4. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM
  5. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 01:08 AM