473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

New to Strings Please help

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
2 1410
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
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

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

Similar topics

5
by: Charles Law | last post by:
Hi folks Not really a .NET question, but I always think this is a good place to ask. Does anyone have a favourite algorithm for determining the similarity of, or difference between two...
6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
9
by: Diane | last post by:
Could you please explain me how can I output nested strings? Here is an example: "adsd{rfkm}xcv" The output should start from the inner parentheses, such as: dfF rfkm
5
by: Twinkle | last post by:
HI guys i have two strings .in first string a word file is there and the second string a html file is there.now i want to compare to both string if some word missing in second string then it...
17
by: kleary00 | last post by:
Hi, I am writing a function that needs to return an array of strings and I am having some trouble getting it right. I need some help. Here is what I consider an array of 100 strings: char...
16
by: InDepth | last post by:
Now that .NET is at it's fourth release (3.5 is coming soon), my very humble question to the gurus is: "What have we won with the decision to have string objects immutable? Or did we won?" ...
5
by: Kelth.Raptor | last post by:
Im having some difficulty with strings here, I hope someone is kind enough to help, I do appreciate it. Im working on a grade point average calculator for my intro to programming class and I...
19
by: bowlderyu | last post by:
Hello, all. If a struct contains a character strings, there are two methods to define the struct, one by character array, another by character pointer. E.g, //Program for struct includeing...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.