954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Strings Program

Hi I need help in programming with strings....I started a code, but I'm not sure how to do it....Please helpread in one string which consists of two words Ex. "Computer Science"
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.
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.
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.
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”
#include<iostream>
#include<string>
using namespace std;

int main()
{
string [50];
string a ("Computer Science");
cout<<"Computer Science";

makewords( int a, int b, int c);


string[50];

limergal
Newbie Poster
11 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

So where is the remaining code...?

Post it to get more help.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

> makewords( int a, int b, int c);
How does this match your description of a function taking 3 strings?

Read up on the string class.
There is plenty of functionality to say
- find a space in a string
- return sub-strings of a string.
- get the length of a string.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

string [50] makes no more sense than int[50]. Given you have included the string header file you supposedly will be using STL strings, which is fine. Under that scenario you could declare a single string like this:

string s;

or an array of strings like this:

string s[50];

but

string[50] doesn't cut the mustard.

To read a string that contains white space into an STL string use the following syntax:

getline(streamYouAreUsing, stringToStoreInputIn, terminatingChar);

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

Little confused here, the std::string does more or less what you're asking.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

oh sorry I meant to write that in before String s[50],

int main()
{
string s[50];
string a ("Computer Science");
cout<<"Computer Science";
 
makewords( int a, int b, int c);
 
 
string[50];



with calling in a fuction with 3 parameters, I did that, but I don't get what do to after. Do I even have the top portion correct, with the string (Computer Science)

limergal
Newbie Poster
11 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

well, I'm only supposed to read in 2 words, I guess that's what the program is saying, (Computer Science)

limergal
Newbie Poster
11 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

To me, the phrase "read in" means accept input from some stream; keyboard input , file input, whatever, not hard code with static string.

makewords() shouldn't take three ints, it should take three strings.

In your programs, if you make a mistake once, you may well make it again, as in your use of string[50. You need to correct each instance of the error, not just one.

Don't have two variables by the same name in any given program. That is don't have a string called a and an int called a.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

oh okay I get what you mean about that,

so

int main(){
 
string[50];
string("Computer Science")
 
 
makewords() /* do I have to declare a string length here
(e.g 20,50,100 etc)?*/
 
string( )    /*In this I have  to put in 3 strings?*/
limergal
Newbie Poster
11 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

I don't know I guess I can't get it......what am I doing wrong?How do I make it even work? I'm not sure

limergal
Newbie Poster
11 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

You're still using the string[50] syntax. If you don't stop it soon I'll have to assume you are doing it on purpose to irritate me.

This,

string("Computer Science");

is essentially the same problem. All variables need to be declared with a type and a name. If you don't give variables a name it's an error, period.

Likewise, this instruction:

"call a function makewords() which receives three parameters: a string which holds the original phrase and two strings to hold the two words"

requires that makewords() is passed three strings. You just pass the string. You don't need to worry about the length of the strings when passing them.

You should also be aware that functions need to be declared, though not necessarily defined before they are called. If you define a function before main, then you don't need a formal declaration. So, it's either:

#include <iostream>
#include <string>
using namesspace std;

//formal declaration of foo before main(), define foo after main()
void foo(string);

int main()
{
   //declare a string with the name string1
   string string1 = "Hello";

   //call the function passing it a string
   foo(string1);
}

//define the function called foo
void foo(string x)
{
    cout << x;
}

OR

#include <iostream>
#include <string>
using namesspace std;

//define the function called foo before main(), no formal declaration needed
void foo(string x)
{
    cout << x;
}

int main()
{
   //declare a string with the name string1
   string string1 = "Hello";

   //call the function passing it a string
   foo(string1);
}
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

Oh I'm so sorry if I irrated you, I didn't mean too, before I couldn't get it in my head, but now with what you gave me I understand better, I really appreciate you taking the time out of your day to help me.....

So I would like to say Thank You, you've been really helpful, and had much patience with me.

limergal
Newbie Poster
11 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You