String program Examples in C++


Content 


1. Program to Input and Display String

#include <iostream.h>
#include <conio.h>
#include <stdio.h>

void main()
{
clrscr();
char c[25];

cout << " Enter Name : ";
gets(c);

cout << "\n Name = " << c;
cout << "\n Name = ";
puts(c);

getch();
}

Output:

Enter Name : Easy Programming24

Name = Easy Programming24
Name = Easy Programming24





2. Program to find Length of String without using strlen() function

#include <iostream.h>
#include <conio.h>
#include <stdio.h>

void main()
{
clrscr();
char str[50];
int  i, len;

cout << " Enter String : ";
gets( str );

for( i=0 ; str[ i ] != '\0' ; i++ );
len = i;

cout << "\n Length of String : " << len;
getch();
}

Output:

Enter String : Easy Programming24
Length of String :18





3. Program to Copy a String using strcpy() function

#include <iostream.h>
#include <conio.h>
#include <string.h>

const int MAX = 80;

void main()
{
clrscr();
char str1[ ]= "Easy Programming24";

char str2[MAX];
  strcpy(str2, str1);

cout << endl;
   cout << str2;

getch();
}

Output:

Easy Programming24





4. Program to Concat Strings using strcat() function

#include <iostream.h>
#include <string.h>
#include <conio.h>

void main()
{
clrscr();
char name[60];

strcpy(name, "Easy");
strcat(name, " ");
strcat(name, "Programming");
strcat(name, " ");
strcat(name, "24");

cout << "\n The String is = " << name;

getch();
}

Output:

The String is = Easy Programming 24





5. Program to Swap Two Strings.

#include <iostream.h>
#include <conio.h>
#include <string.h>

void main()
{
clrscr();
char str1[25], str2[25], tmp[25];

cout << "\n Enter Two String : ";
cin >> str1 >> str2;

cout << "\n Strings Before Swapping are : \n";
cout << " 1. " << str1 << ", 2. " << str2;

strcpy( tmp, str1 );
strcpy( str1, str2 );
strcpy( str2, tmp );

cout << "\n Strings After Swapping are  : \n";
cout << " 1. " << str1 << ", 2. " << str2;

getch();
}

Output:

Enter Two String : Easy Programming24

Strings Before Swapping are :
1. Easy,  2. Programming24

Strings After Swapping are :
1. Programming24,  2. Easy




6. Program to Reverse a String without using String function

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

void main()
{
clrscr();
char str1[25], str2[25];
int i, j, l;

cout << "\n Enter a string : ";
gets(str1);

for(i=0 ; str1[ i ] != '\0' ; i++) ;
l = i;
for(i=0,j=l-1 ; j>=0 ; j--,i++)
{  
str2[ i ] = str1[ j ];
}
str2[ i ] = '\0';

puts(str2);
getch();
}

Output:

Enter a string : Easy Programming24

42gnimmargorP ysaE






7. Program to count Number of Words and Number of Characters in Given String

#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>

void main()
{
clrscr();
char str1[100];
int i, j, k, c = 0, w = 1;

cout << "\n Enter Sentences ::: ";
gets( str1 );

i = strlen( str1 );
for( i=0 ; str1[ i ] != '\0' ; i++ )
{
if( ( str1[ i ] == ' ' ) && ( str1[ i-1] != ' ' ) )
w++;
else if( str1[ i ] != '.' )
c++;
}

cout << "\n Number of words ::: " << w;
cout << "\n Number of characters ::: " << c;

getch();
}

Output:

Enter Sentences : Easy Programming24

Number of words : 2
Number of characters : 18






8. Program to count Number of Vowels and Consonants

#include <iostream.h>
#include <conio.h>
#include <stdio.h>

void main()
{
clrscr();
char line[81];
int vowctr = 0, conctr = 0;

cout << " Enter a String (max. 80 character) : " << endl;
gets(line);

for(int i=0 ; line[ i ]!='\0' ; i++)
{
switch(line[ i ])
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
    vowctr++;
break;
default:
conctr++;
break;
}
}

cout << "\n The Number of Vowels = " << vowctr;
cout << "\n The Number of Consonants = " << conctr;
cout << endl;

getch();
}

Output:

Enter a String (max. 80 character) : Easyprogramming

  The Number of Vowels = 4
  The Number of Consonants = 11





9. Program to check whether input character is Capital letter, Small letter, Digit or Special Symbol

#include <iostream.h>
#include <conio.h>
#include <ctype.h>

void main()
{
char c;
clrscr();

cout << " Enter a character : ";
cin >> c;

if( isupper( c ) )
cout << "\n Character " << c << " is in Upper Case.";
else if( islower( c ) )
cout << "\n Character " << c << " is in Lower Case.";
else if( isdigit( c ) )
cout << "\n Character " << c << " is a Digit.";
else
cout << "\n Character " << c << " is a Special Symbol.";

getch();
}

Output:

Enter a character : E

Character E is in Upper Case.


Reactions

Post a Comment

0 Comments