Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 131,715 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,400 people online right now. Registration is fast and FREE... Join Now!




String Concatenation

 
Reply to this topicStart new topic

String Concatenation

Mo821
post 10 Dec, 2005 - 10:00 AM
Post #1


New D.I.C Head

*
Joined: 10 Dec, 2005
Posts: 8


My Contributions


this is my assignment

take arguments of type string( pass by value or reference

return the concatenation function with entered strings

print to screen concentation of the entered string and its length. main () must be used thereturn value of the concatenate function to get lenght of resulted string.


#include <cstring>
#include <iostream>

using namespace std;

int main()
{
char ch;
int line_count = 0;

char *s1 = new char[999];
char *s2 = new char[999];
char *s3 = new char[999];

cout << "Please enter a string: " << endl;
cin >> s1;

cout << " Please enter a second string: " << endl;
cin >> s2;
cout << strcat(s1, s2)<< endl;



if ( s1 == s2)
cout << "The concatenation of " << endl;
}


if some one can help me out, that would be awesome in correcting this program. Thanks!

Mo
User is offlineProfile CardPM

Go to the top of the page

Mrafcho001
post 10 Dec, 2005 - 10:13 AM
Post #2


D.I.C Addict

Group Icon
Joined: 1 Nov, 2005
Posts: 753



Thanked 5 times

Dream Kudos: 120
My Contributions


what do you mean by "concentration"?

you can also use the string class... It gives you a lot of useful functions. Such as string.size() returns the size of the string, which you need.

EDIT:

nvm im an idiot, and cant read for my life.


you can always do something like this:

CODE

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


int main()
{
    string str1, str2;
    cout << "INPUT str1: ";
    getline(cin, str1, '\n');
    cout << "INPUT str2: ";
    getline(cin, str2, '\n');

    str1.append(str2);
    cout << str1 << endl;

    return 0;
}


you can use str1.size() to get hte string size.

This post has been edited by Mrafcho001: 10 Dec, 2005 - 10:22 AM
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 11 Dec, 2005 - 12:10 AM
Post #3


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,895



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


QUOTE(Mo821 @ 11 Dec, 2005 - 12:27 AM)
char *s1 = new char[999];
char *s2 = new char[999];
char *s3 = new char[999];

*Ouch*
I think even 255 Characters are sufficient for each string at the most.

Besides, you arent using s3 at all.Though the compiler wont include it in the executable file it is good practice to keep code clean.

Comparing strings with the '=' operator wont get you anywhere if you want its length ( and even otherwise...use strcmp )
Use the strlen Function for its length.
For eg.
CODE

cout<<strlen(strcat(s1,s2));

will return the length of the concactenated string.
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 11 Dec, 2005 - 08:21 AM
Post #4


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,163



Thanked 32 times

Dream Kudos: 25
My Contributions


I have to agree with Mrafcho001 on this one...if you using C++, then you should use the string object instead of character arrays.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 09:50AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month