Thread: Problem with Strings, Please help!

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    11

    Problem with Strings, Please help!

    Hello,
    I have a problem where I need to call the function matchexact() passing the two strings. matchexact() will determine how many times the corresponding positions in the two strings hold exactly the same characters. The function will print this value.
    For example, if the two strings are "marriage" and "gerbil", then the function matchexact will return 2, since the two strings have the same characters in positions 2 and 5.
    then I need a function that joins two string into one:
    The main program will then call a function jointhem() which receives the two strings. The function will join the two strings together and print the new string. Ex. if string1 is “bath” and string2 is “water” than the new string is “bathwater”

    Here's my code for matching characters in strings but it doesnt work or maybe its not even being properly called from main.
    Can someone please tell me what is the proper way to do this?

    Code:
    void matchexact(string first, string second){
         int count = 0;
         for (int i = 0; i < second.length(); i++){
             if (first[i] == second[i]){
                          count++;
                          }
         }
    cout << "count: " << count << endl;
    return;
    }
    The function that joins 2 strings when it is called from main doesnt work (it only works if I put this function into mathexact() function.)
    Here it is:
    Code:
    void jointhem(string first, string second){
          string concate
          concate = first + second;
          cout << concate << endl;
    return;
    }
    Here's my full code:
    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    using namespace std;
    void makewords(string ,string ,string);
    void matchexact(string, string);
    void jointhem(string, string); 
    int main (){  
         string phrase, first, second;   
         makewords(phrase, first ,second);
         matchexact(first, second);
         jointhem(first , second);
         system("pause");
    }     
    void makewords(string ,string ,string){
         int pos, pos2, firstsize, secondsize;
         string phrase, first, second;
         getline(cin,phrase);
         pos = phrase.find(" ", 0);
         first = phrase.substr(0,pos);
         second = phrase.substr(pos+1);
         firstsize = first.length();
         secondsize = second.length();
                    if (firstsize == secondsize){
                       cout << "These strings are equal \n";
                    }else{
                       cout << "These strings are not equal \n";      
    }
    cout << "string: " << first << "  size: " << firstsize << endl;
    cout << "string: " << second << "  size: " << secondsize << endl;
    return;
    }
    void matchexact(string first, string second){
         int count = 0;
         for (int i = 0; i < 4; i++){
             if (first[i] == second[i]){
                count++;
             }
         }
    cout << "count: " << count << endl;
    return;
    }
    void jointhem(string first, string second){
          string concate;
          concate = first + second;
          cout << concate << endl;
    return;
    }
    Please help me put this together. Thank you.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Pass by reference.
    Code:
    void matchexact(string first, string second){
         int count = 0;
         for (int i = 0; i < 4; i++){
    Wouldn't you want the smaller of the two lengths?
    Code:
    void matchexact(string &first, string &second)
    {
       int count = 0;
       for ( int i = 0; i < min(first.size(), second.size()); i++ )
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    11
    pass by reference doesnt seem to change anything with either functions, still gives wrong result like some random number for matching ( so "for ( int i = 0; i < min(first.size(), second.size()); i++ )" probably won't work) and joinit() function that joins 2 strings makes blank space. In fact these functions dont even run properly when called from main, but when I put both functions matchexact() and jointhem() into makewords() function then they start computing - in this case joinit works, but matcheaxact() gives wrong result. Is there something wrong with how my functions are called from main?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I'll let you sort through the several changes here...
    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    using namespace std;
    void makewords(string& ,string& ,string&);
    void matchexact(string&, string&);
    void jointhem(string&, string&); 
    int main ()
    {
       string phrase, first, second;   
       makewords(phrase, first ,second);
       matchexact(first, second);
       jointhem(first , second);
       system("pause");
    }     
    void makewords(string &phrase, string &first, string &second)
    {
       int pos, pos2, firstsize, secondsize;
       getline(cin,phrase);
       pos = phrase.find(" ", 0);
       first = phrase.substr(0,pos);
       second = phrase.substr(pos+1);
       firstsize = first.length();
       secondsize = second.length();
       if ( firstsize == secondsize )
       {
          cout << "These strings are equal \n";
       }
       else
       {
          cout << "These strings are not equal \n";      
       }
       cout << "string: " << first << "  size: " << firstsize << endl;
       cout << "string: " << second << "  size: " << secondsize << endl;
       return;
    }
    void matchexact(string &first, string &second)
    {
       int count = 0;
       for ( int i = 0; i < min(first.size(), second.size()); i++ )
       {
          if ( first[i] == second[i] )
          {
             count++;
          }
       }
       cout << "count: " << count << endl;
       return;
    }
    void jointhem(string &first, string &second)
    {
       string concate;
       concate = first + second;
       cout << concate << endl;
       return;
    }
    
    /* my output
    marriage gerbil
    These strings are not equal 
    string: marriage  size: 8
    string: gerbil  size: 6
    count: 2
    marriagegerbil
    Press any key to continue . . . 
    */
    Please don't copy-and-paste if this is homework -- try to learn the differences.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    11

    Smile

    Its weird I was running exactly same thing several times and it didnt work. Thanks a lot for your help.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    All righty, I'll highlight the differences:
    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    using namespace std;
    void makewords(string& ,string& ,string&);
    void matchexact(string&, string&);
    void jointhem(string&, string&); 
    int main ()
    {
       string phrase, first, second;   
       makewords(phrase, first ,second);
       matchexact(first, second);
       jointhem(first , second);
       system("pause");
    }     
    void makewords(string &phrase, string &first, string &second)
    {
       int pos, pos2, firstsize, secondsize;
       //string phrase, first, second; // <<removed>>
       getline(cin,phrase);
       pos = phrase.find(" ", 0);
       first = phrase.substr(0,pos);
       second = phrase.substr(pos+1);
       firstsize = first.length();
       secondsize = second.length();
       if ( firstsize == secondsize )
       {
          cout << "These strings are equal \n";
       }
       else
       {
          cout << "These strings are not equal \n";      
       }
       cout << "string: " << first << "  size: " << firstsize << endl;
       cout << "string: " << second << "  size: " << secondsize << endl;
       return;
    }
    void matchexact(string &first, string &second)
    {
       int count = 0;
       for ( int i = 0; i < min(first.size(), second.size()); i++ )
       {
          if ( first[i] == second[i] )
          {
             count++;
          }
       }
       cout << "count: " << count << endl;
       return;
    }
    void jointhem(string &first, string &second)
    {
       string concate;
       concate = first + second;
       cout << concate << endl;
       return;
    }
    
    /* my output
    marriage gerbil
    These strings are not equal 
    string: marriage  size: 8
    string: gerbil  size: 6
    count: 2
    marriagegerbil
    Press any key to continue . . . 
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    11
    Thanks for the highlitings Dave, but I noticed all pass by references, removed strings and min(first.size(), second.size()). When I made those adjustments to my code, it still wouldnt run, but when I copy pasted your code it worked. I must've missed something minor, just very awkward. Just one thing, I'd appreciate it if you could briefly clear up for me why pass by reference needs to be used in this case, and in what cases it needs to be used in general? I thought that pass by reference is only used when multiple things need to be returned and this is a void function.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    It's a better habit, generally. But look at your main:
    Code:
    int main ()
    {
       string phrase, first, second;   
       makewords(phrase, first ,second);
       matchexact(first, second);
       jointhem(first , second);
       system("pause");
    }
    You want functions that change these strings to have the changes retained when you return from the function(s) that make (a) change(s). You want functions that don't change these strings to pass relatively efficently -- that is, don't make a copy if you don't need to. The ones that don't change the string would be better served using a const string &.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    11
    Oh I see, makes sense. Thank you for all your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 08:01 PM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 12:01 PM
  4. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 08:05 PM