Putting a string in another string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shisou
    New Member
    • Jul 2007
    • 52

    Putting a string in another string

    While the title of this seems easy i fear it might not be :\

    I'm trying to have my program search through a string to find the character '-' when it finds it, it will look at the next character and based on what that is it will run a certain function.

    i.e.
    Code:
     if(string[i+1] == 'a'){
                functiona(int i);
    }
    in the function i want it to replace the "-a" with a word chosen at random, here is where i run into an issue.

    is there a way that i can have the word placed into the string and the rest of the characters in the string moved down accordingly?

    so if my string was "hello -a world!" and i wanted to put something like "small" starting at the '-' the end result would be "hello small world" instead of "hello smallrld"

    Thanks in advance for any advice :)

    p.s. i apologize for the use of hello world... so overused.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    There is no standard library function to do this, you will have to hand code it.

    Comment

    • Studlyami
      Recognized Expert Contributor
      • Sep 2007
      • 464

      #3
      That is if your using C (which it looks like you are). In C++ the string class has a find function and a insert function which would do what you want.

      Comment

      • Shisou
        New Member
        • Jul 2007
        • 52

        #4
        unfortunately I am working in C so it looks like more work for me :P

        Thanks for the help

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          strchr() anyone?

          strchr returns the address of the first occurrance of the character in the string.

          You get the index location by subtracting the address of the string from the address returned by srtrchr().

          Comment

          Working...