By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 2,283 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

New to Strings Please help

P: 12
Hello,
I just started learning strings and dont even know how to properly execute simple part of the program that splits phrase of two words with one space in between into 2 separate strings. I realize that substr() need to be used, but words may varie in length so I have to make it initialize first string any length before first space and second string after the space.
For example: "computer science"
into first string "computer" second string "science"
Here's whats being asked of me:
call a function makewords() which receives three parameters: a string which holds the original phrase and two strings to hold the two words. The function will break the string stored in the first parameter (the original sentence) into two words and then store it in one of the parameters.
Note: The function can assume there is exactly one space between the words in the sentence.
For example, if the original sentence holds: "computer science", then the two words are "computer" and "science”.

You should use member functions such as find and substr to help you with this function
Your function, makewords(), should then check if the 2 new strings are equal to each other, and print the appropriate message saying whether or not they are equal.
print out the two words and their sizes.
Here's my starting piece of code:

Expand|Select|Wrap|Line Numbers
  1. void makewords(string ,string ,string){
  2. string phrase;
  3. getline(cin,phrase);
  4. string first, second;
  5.  
  6.  
  7. system("pause"); 
  8. }

Any help is appreciated!
Nov 27 '06 #1
Share this Question
Share on Google+
2 Replies


P: 12
I was able to solve it, and now Im stuck at new piece of code:
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.


My logic is it would be something like this but it doesnt work correctly.
Expand|Select|Wrap|Line Numbers
  1. void matchexact(string first, string second){
  2.      int count = 0;
  3.      for (int i = 0; i < ' '; i++){
  4.          if (first[i] == second[i]){
  5.                       count++;
  6.                       }[
What am I doing wrong?

Here's the whole code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. using namespace std;
  5. void makewords(string ,string ,string);
  6. void matchexact(string, string);
  7. void jointhem(string, string); 
  8. int main (){  
  9. string phrase, first, second;    
  10. makewords(phrase, first ,second);
  11. matchexact(first, second);
  12. jointhem(first, second);
  13. system("pause");
  14. }     
  15. void makewords(string ,string ,string){
  16. int pos, pos2, firstsize, secondsize;
  17.      string phrase, first, second;
  18.      getline(cin,phrase);
  19.      pos = phrase.find(" ", 0);
  20.      first = phrase.substr(0,pos);
  21.      second = phrase.substr(pos+1);
  22.      firstsize = first.length();
  23.      secondsize = second.length();
  24.                 if (firstsize == secondsize){
  25.                    cout << "These strings are equal \n";
  26.                 }else{
  27.                    cout << "These strings are not equal \n";      
  28. }
  29. cout << "string: " << first << "  size: " << firstsize << endl;
  30. cout << "string: " << second << "  size: " << secondsize << endl;
  31. }
  32. void matchexact(string first, string second){
  33.      int count = 0;
  34.      for (int i = 0; i < ' '; i++){
  35.          if (first[i] == second[i]){
  36.                       count++;
  37.                       }
  38.      }
  39. cout << "count: " << count << endl;
  40. }
  41.  
  42. void jointhem(string first, string second){
  43.       string concave;
  44.       concave = first + second;
  45.       cout << concave << endl;
  46. return;
  47. }
  48.  
  49.  
Nov 28 '06 #2

vpawizard
P: 66
I was able to solve it, and now Im stuck at new piece of code:
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.


My logic is it would be something like this but it doesnt work correctly.
Expand|Select|Wrap|Line Numbers
  1. void matchexact(string first, string second){
  2.      int count = 0;
  3.      for (int i = 0; i < ' '; i++){
  4.          if (first[i] == second[i]){
  5.                       count++;
  6.                       }[
What am I doing wrong?

Here's the whole code:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4. using namespace std;
  5. void makewords(string ,string ,string);
  6. void matchexact(string, string);
  7. void jointhem(string, string); 
  8. int main (){  
  9. string phrase, first, second;    
  10. makewords(phrase, first ,second);
  11. matchexact(first, second);
  12. jointhem(first, second);
  13. system("pause");
  14. }     
  15. void makewords(string ,string ,string){
  16. int pos, pos2, firstsize, secondsize;
  17.      string phrase, first, second;
  18.      getline(cin,phrase);
  19.      pos = phrase.find(" ", 0);
  20.      first = phrase.substr(0,pos);
  21.      second = phrase.substr(pos+1);
  22.      firstsize = first.length();
  23.      secondsize = second.length();
  24.                 if (firstsize == secondsize){
  25.                    cout << "These strings are equal \n";
  26.                 }else{
  27.                    cout << "These strings are not equal \n";      
  28. }
  29. cout << "string: " << first << "  size: " << firstsize << endl;
  30. cout << "string: " << second << "  size: " << secondsize << endl;
  31. }
  32. void matchexact(string first, string second){
  33.      int count = 0;
  34.      for (int i = 0; i < ' '; i++){
  35.          if (first[i] == second[i]){
  36.                       count++;
  37.                       }
  38.      }
  39. cout << "count: " << count << endl;
  40. }
  41.  
  42. void jointhem(string first, string second){
  43.       string concave;
  44.       concave = first + second;
  45.       cout << concave << endl;
  46. return;
  47. }
  48.  
  49.  


In the loop condition you are comparing i with ' ' (space-ASCII 32). So, the loop runs 32 times irrespective of length of string causing the program to read beyond array boundaries. Instead, you can first determine which of the string is shorter and run the loop, length of shorter string times.
Nov 28 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.