C++ program to concatenate strings




cpp tutorials
cpp tutorials

Here we will learn how to write a program to Concatenate strings.

CONCATENATION OF TWO STRINGS USING C++ PROGRAM

# include < iostream>
# include < string >
using namespace std;
int main()

{
char str1[50], str2 [35];
cout << "Enter string str1;";
cin >> str1;
cout << "Enter string str2:";
cin >> str2;
strcat(str1,str2);
cout << "strcat (str1, str2 ) : "<< str1;

    system("pause");
return 0;
}

 


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





1 Comment

  1. I have a heavy fight with string Constant in c/c++, and I would like to know the cleanest and most elegant way to achieve the goal.
    A given string may be used in more than one place in your program. It is good programming praxis to define a string constant in one place and then use it ever where you need. Hence in Java I would say:
    public/private static final String MY_STRING_REF = “whatever I need”;

    It’s obvious, that if I must change the String to say “whatever I need now!”, I change it exactly in one place and with the next compiler run the new string is available where ever it was referenced. Beside the string value is stored exactly once in the program space.

    I would like to apply this praxis to c++ too. But if your c++ programs use old structures from c, things turn ugly!
    Example:
    JavaVMOption* options = new JavaVMOption[1]; // JVM invocation options
    options[0].optionString = (char *)CLASSPATH;
    Yes, this is old JNI interface to access OS-services from Java through c++

    I have found a kind of declaration without complains from the compiler as:
    const char * CLASSPATH = “-Djava.class.path=.:./xx”;

    This works. But I don’t like the type cast and I would be happy to know a more elegant way to achieve it. Maybe this could be a good example for your tutorial site.

Leave a Reply

Your email address will not be published.


*