C++ | Using strings and concatenation of strings.

Introduction

This is a tutorial for using strings in c++. The program is given below that prints your name. The program is extendable. Go enjoy the program. Lets begin……

Program to use strings and do concatenation of strings

#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type char
char s[100],*str;
char ss[100],*sstr;
char *ssstr;
//Input the string
cout<<“Enter your name.”<<endl;
gets(s);
cout<<“Enter your surname.”<<endl;
gets(ss);
//process
int l = strlen(s);
int ll = strlen(ss);
str = new char[l+1];
strcpy(str,s);
sstr = new char[ll+1];
strcpy(sstr,ss);
//join
ssstr = new char[l+ll+3];
strcpy(ssstr,str);
strcat(ssstr,” “);
strcat(ssstr,sstr);
//show the output
cout<<“Hello, “<<ssstr;
//get character
getch();
}

Output

Enter your name.
Abc
“Enter your surname.
Xyz
Hello, Abc Xyz

How it works

  1. You enter your name and surname
  2. Program does concatenation of your name and surname.
  3. Program says hello to you.

Extending it

The program can be extended by using the concept in any program.

  • Remember this is a basic part of c++ which can be used in any program….

Explanation.

  1. Include ‘iostream.h’, ‘conio.h’, ‘stdio.h’ and ‘string.h’ files.
  2. Add void main.
  3. Start program by first clearing the screen.
  4. Delcare variables.
  5. Add the gets() funtion for getting string.
  6. Concat string.
  7. Print the string.

At the end

You learnt creating the C++ program for using strings and concatenation of strings in c++. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s