Thread: Need help with what should be a simple program

  1. #1
    Registered User
    Join Date
    Sep 2009
    Location
    Murfreesboro, TN
    Posts
    5

    Need help with what should be a simple program

    Hi guys,

    Need some basic help here. Basically I have to write a program that will search a string and replace all occurrences of "C++" in that string with "Java". The program will then have to convert the updated string to upper case letters. I've got it working great for strings that have more than one instance of "C++" but if the string only has it in there once it messes up. Can anyone give me some guidance?

    Here is my code:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main( )
    {
        string    original;      //contains input string
        string    modified;    //the string obtained by replacing "C++" with "Java"
        string    upperStr;    //convert chars in the string "modified" to upper case
        int        pos;        //the starting position of the substr "C++" 
        
    
        getline(cin, original);
    
    // Anything above this cannot be edited because it was all ready in the code given to us
    
    string temp;
    
    pos = original.find("C++");
    
    modified = original.substr(0, pos) + "Java";
    
    temp = original.substr(pos + 3);
    
    pos = temp.find("C++");
    
    modified += temp.substr(0, pos) + "Java" + temp.substr(pos + 3);
    
    upperStr = modified;
    
    for (int i = 0; i < upperStr.length(); i++)
    {
    upperStr[i] = toupper(upperStr[i]);
    }
    
    // End of editable code
     cout << "The original string is: \n\t" << original << endl
             << "After replacing C++ with Java: \n\t" << modified << endl
             << "Converting to upper case: \n\t" << upperStr << endl;
             
        return 0;
    }
    Any help would be greatly appreciated!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Every time you call find() on a string, you need to check if it returns npos. If it returns npos, then that means the substring was not found, and you can stop your processing.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Location
    Murfreesboro, TN
    Posts
    5
    Thanks! I did some research on the npos and actually got it working perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM