-3

I have a very confusing homework assignment that I am not sure how to do correctly. The assignment asks to complete a C++ function to parse a string and swap all occurrences of the substring oldStr with instances of the string newStr. The inputStr may need to change in size due to the result of the replacement...meaning that oldStr and newStr do not have to be the same size. A function header is provided. I think that it is a search and replace function but I am not sure if that is even a correct assumption. I also get errors that inputStr must have a class type? And left of .find and .replace must have class/struct/union? Any suggestion would be greatly appreciated.

void parseSwap( char* inputStr, const char* oldStr, const char* newStr  )
{
size_t oldStrLength = strlen(oldStr);
size_t newStrLength = strlen(newStr);
size_t position = 0;

while ((pos = inputStr.find(oldStr, position)) != string::npos)
{
  inputStr.replace( position, oldStrLen, newStr );

  position += newStrLen; 

}
Nick
  • 1,036
  • 2
  • 14
  • 27
  • 1
    You're mixing `char *` and `std::string` haphazardly. – chris Jul 05 '13 at 17:09
  • You know, why don't you use std::string instead of char? And check out http://stackoverflow.com/questions/1494399/how-do-i-search-find-and-replace-in-a-standard-string – kirbyfan64sos Jul 05 '13 at 17:09
  • 1
    Is this the function header provided in the assignment? – n. m. could be an AI Jul 05 '13 at 17:10
  • That is the function header....it is not a mistake the inputStr is a char* even though it would make life a lot easier if it were std::string. – Nick Jul 05 '13 at 17:12
  • If you had paid closer attention to the answer [to your own question on this subject](http://stackoverflow.com/questions/17456570/parse-string-and-swap-substrings) you would notice that the answer from Mudbuddy uses a std::string& as a first parameter not char*. That is why the calls to .find and .replace don't work. Those are methods of std::string. – Borgleader Jul 05 '13 at 17:12
  • like I said char* is in the function header...it is not an option to use std::string& for my inputStr variable – Nick Jul 05 '13 at 17:13
  • @Nick It's impossible to implement the function correctly given this function signature; if you are to modify `inputStr`, you must know the size of the buffer it points to. (And anyone who would name an in-out parameter `inputStr` shouldn't be allowed to program). – James Kanze Jul 05 '13 at 17:45

3 Answers3

1

You are using C style strings (char*) as if they were C++ strings (string).

C parts:

  • strlen() - function used to find the length of a C string
  • inputStr, oldStr, newStr are all C strings

C++ parts:

  • string is class in C++, which represents a string.
  • The functions find(), replace(), length() are available in string
  • The value string::npos

This C++ code should do the job. You need to include the headers <iostream> and <string>.

void parseSwap(string& inputStr, const string& oldStr, const string& newStr)
{
    size_t oldStrLen = oldStr.length();
    size_t newStrLen = newStr.length();
    size_t position = 0;

    while ((position = inputStr.find(oldStr, position)) != string::npos)
    {
      inputStr.replace( position, oldStrLen, newStr );

      position += newStrLen; 

    }
}

If you cannot change the function arguments and still want to use your old code, use this. You still have to include the above headers though.

void parseSwap( char* inputStr, const char* oldStr, const char* newStr  )
{
    string input_string(inputStr);
    string old_string(oldStr);
    string new_string(newStr);
    size_t position = 0;

    while ((position = input_string.find(old_string, position)) != string::npos)
    {
      input_string.replace( position, old_string.length(), new_string );
      position += new_string.length();
    }

    strcpy(inputStr, input_string.c_str());
}
max
  • 4,248
  • 2
  • 25
  • 38
  • Good luck trying to pass string literals or anything as the second and third arguments, though. – chris Jul 05 '13 at 17:15
  • If I could do it this way I would but the function header is provided as I have it in my code. I cannot change the char* inputStr to a string& inputStr. It is not an option. – Nick Jul 05 '13 at 17:16
  • You should read about all the things used above. You could start by reading about the `string` class: http://www.cplusplus.com/reference/string/string/ – max Jul 05 '13 at 17:51
  • 1
    This doesn't work if `newStr` is longer than `oldStr` and the client hasn't provided a sufficiently large buffer. If the function is to write through `inputStr`, the interface _must_ provide the size of the buffer, or the function is broken. (In the worst case, you can assume that the buffer is `strlen(inputStr) + 1`, and no bigger.) – James Kanze Jul 05 '13 at 17:54
0

I'm going to write this assuming that using built-in regex/string-replace functions don't fulfill the assignment.

Search and replace isn't going to cut it if oldStr and newStr aren't going to be the same size. You're going to have to completely regenerate the string. or at the very least move all the values over(regenerating it is easier).

What I would do, is create a new string, and start filling that up with what's in the old string except with the replacements.

You can use a DFA to identify the parts of the string that you need to replace

My thinking would be that you can start trying to match the string, and if you find the match, append the newstr to your new string, and if you find that it doesn't match, append the part that's not a match to newstr

0

Yes, this is search and replace but you need to do more than rely on existing routines to account for varying sizes of substrings.

char* inputStr;
inputStr.find(…

Your errors are because you're treating char * as if it were a std::string object. Don't do that.

mah
  • 39,056
  • 9
  • 76
  • 93