Am trying to build a function that will take string, sub_string that will be replaced, and new string that will replace that sub_string. Now before you all go with strstr, i dont want to use <string>. Anyway I got my code working to find a index of a char array from rewritting should start. consider this code :

string replace_string(char *original, char *new_n, int index,int size_of_org,int size_of_sub, int size_of_new) {

// original - string provided by user
// new_n - string to replace a sub_string
// index - index of a 1st character of a substring
// size of user provided string, size of sub_string, size of string that will replace sub_string

      int d = 0;
      int difference_in_size;
      int Size;

      difference_in_size = size_of_new - size_of_sub;   // used if sub_string and new_n are not same lenght 

       Size = size_of_org + difference_in_size;  // calculating size for our string that will be ouput in the end

      char new_char_niz[Size];  

      cout << "size : " << Size <<endl;

      for(int x = 0; x < size_of_org +1  ; x++){

         new_char_niz[x] = original[x];              // am copyng a user provided string to our newly created char array

      }

      //for(int y = index;y < size_of_new+1; y++){   // Part I cant figure out.

        //new_char_niz[y] == new_n[d++];              // starting at target index, copy every element to our new string

      //}

      string new_string(new_char_niz);              // convert to string
      return new_string;
}

Any suggestion for solution of this issue is a big Thank You.

Recommended Answers

All 3 Replies

Why on earth would you return a string from this function and not take strings? A char * can be converted to a string so you should use strings. Does this code even compile? I'm not sure what compiler ou are using but line 16 should not compile.

I'll help you out and give you the pusedo code of how this should work if you want to see how to do it with char *'s. I am not putting any error checking in it.

int new_string_size = orig_size + new_sub_size - old_sub_size
char * new_string = new char[new_string_size]

// copy first part of original
for i = 0, i < index_of_substring_to_replace; i++
    new_string[i] = orig[i]

for j = index_of_substring_to_replace, k = 0; j < index_of_substring_to_replace + new_sub_size; j++, k++
    new_string[j] = new_sub[k]

// m is the index of orig after the sub string
for l = index_of_substring_to_replace + new_sub_size, m = index_of_substring_to_replace + orig_sub_size; l++, m++
    new_string[l] = orig[m]

Now before you all go with strstr

I'm not sure I'd recommend strstr for this anyway. ;)

i dont want to use <string>

You mean <string.h>? That's where strstr is declared. <string> defines the std::string class.

Any suggestion for solution of this issue is a big Thank You.

Keep it simple. Search through the source for an instance of the substring, copy all characters up to that point to a new std::string object, the replacement string, then all characters after the substring. Shooting from the hip (please excuse errors):

string replace_string(const char *source, const char *match, const char *replace)
{
    const char *start = source;
    const char *end;
    string result;

    // Find the start of the match string
    while (*start != '\0')
    {
        if (*start == *match)
        {
            const char *p = start;
            const char *q = match;

            // Check for the rest of the match string
            while (*q != '\0' && *p == *q)
            {
                ++p;
                ++q;
            }

            if (*q == '\0')
            {
                // Perfect match, done with this search
                end = p;
                break;
            }
        }

        ++start;
    }

    // Copy up to the matched string
    result.append(source, start - source);

    // Copy the replacement string
    result += replace;

    // Copy after the matched string
    result += end;

    return result;
}

The only hairy part is manually searching for the substring, but it's not exceptionally tricky for an exact match. Of course, you'd also need to add error handling and edge cases, but I left those out to illustrate the basic logic.

#include <iostream>
#include <string>
#include <cstring>

std::string replace(  const char* str, // input string; make that const-correct,
                      std::size_t sz_str, // length of input string
                      std::size_t start, // start pos of substring to be replaced
                      std::size_t length, // length of substring to be replaced,
                      const char* new_substr, // string to replace the substring, made const-correct
                      std::size_t sz_substr ) // length of string to replace the substring,
{
     std::string result( str, str+start ) ; // initialize with chars before the substring
     result.append( new_substr, sz_substr ) ; // append the new substring
     result.append( str+start+length, str+sz_str ) ; // append chars after the substring
     return result ;
}

std::string replace(  const char* str, // input string; make that const-correct,
                      std::size_t start, // start pos of substring to be replaced
                      std::size_t length, // length of substring to be replaced,
                      const char* new_substr ) // string to replace the substring, made const-correct
{
    if( str == nullptr ) return new_substr ? new_substr : "" ;
    auto sz_str = std::strlen(str) ;
    start = std::min( start, sz_str - 1 ) ;
    length = std::min( length, sz_str-start ) ;

    if( new_substr == nullptr ) return replace( str, sz_str, start, length, nullptr, 0 ) ;
    else return replace( str, sz_str, start, length, new_substr, std::strlen(new_substr) ) ;
}


int main()
{
    std::cout << replace( "nhrnsubstr6", 4, 6,"jic" ) << '\n'
              << replace( "substrjic6", 0, 6,"nhrn" ) << '\n'
              << replace( "substrnhrnjic6", 0, 6,"" ) << '\n'
              << replace( "nhrnjic6substr", 8, 100,"" ) << '\n'
              << replace( "nhrnsubstr6jic", 4, 6,"" ) << '\n' ;
}

http://coliru.stacked-crooked.com/a/85b25cc376e515b0

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.