Hi I need help in programming with strings....I started a code, but I'm not sure how to do it....Please help

  1. read in one string which consists of two words Ex. "Computer Science"
  2. 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.
  3. 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.
  4. print out the two words and their sizes.
  5. 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.
  6. 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];

Recommended Answers

All 11 Replies

So where is the remaining code...?

Post it to get more help.

> 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.

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);

Member Avatar for iamthwee

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

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)

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

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.

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?*/

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

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);
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.