C++ Programs Examples With Output [2021 CPP Examples]

C++ programing

String and String Array

Strings are similar to arrays with just a few differences. Usually, the array size is fixed,while strings can have a variable number of elements.Strings are mostly used in programming and so every coder needs to be familiar with strings and its library function.

Strings library functions are there to reduce coding efforts,but a real and passionate coder would like to code in iterative way.The string ans string array examples with some explanation given below describes more on string functionality.

1.Write a program to print reverse of a string.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[50],str2[50];
char *rev;
clrscr();
cout<<"Enter any string \n";
cin>>str;
strcpy(str2,str);
rev = strrev(str);
cout<<"Reverse of "<<str2<<" is "<<rev;
getch();
}

2.Write a program to calculate the number of white space in given string.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int count = 0,i = 0,len = 0;
char str[50];
clrscr();
cout<<"Enter any string \n";
cin.getline(str,sizeof(str));
len = strlen(str);
for (i = 0; i < len; i++)
{
if (str[i] == ' ')
count++;
}
cout<<"Number of white space in given string is "<<count;
getch();
}

3.Write a program to check whether a string is palindrome or not.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int strlength = 0,i = 0,j = 0,flag = 0,count = 0;
char str[50],*str2,strcopy1[50];
clrscr();
cout<<"Enter a string \n";
cin.getline(str,sizeof(str));
strcpy(strcopy1,str);
strlength = strlen(strcopy1);
str2 = strrev(str);
for(i=0; i<strlength; i++)
{
for(j=count; j>=0; j--)
{
if(strcopy1[i] != str2[j])
{
flag=1;
break;
}
count++;
break;
}
if(flag==1)
{
break;
}
}
if(flag==1)
{
cout<<strcopy1<<" is not palindrome";
}
else
{
cout<<strcopy1<<" is palindrome";
}
getch();
}

4.Write a program to check number vowels and consonants in string.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int strlength = 0,i = 0,k = 0,l = 0,count1 = 0,count2 = 0;
char str[50],vowels[50],consonants[50];
clrscr();
cout<<"Enter a string \n";
cin.getline(str,sizeof(str));
strlength = strlen(str);
for (i = 0; i < strlength; i++)
{
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u')
{
count1++;
vowels[k] = str[i];
k++;
}
else
{
count2++;
consonants[l] = str[i];
l++;
}
}
if (count1 > 0)
{
cout<<"\n Number of vowels are : "<<count1<<" and they are : ";
for(i = 0; i < k; i++)
{
cout<<vowels[i]<<" ";
}
}
if (count2 > 0)
{
cout<<"\n Number of Consonants are : "<<count2<<" and they are : ";
for(i = 0; i < l; i++)
{
cout<<consonants[i]<<" ";
}
}
getch();
}

5.Write a program to remove a specific character from string.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int strlength = 0,i = 0,k = 0;
char str[50],ch,new_word[50];
clrscr();
cout<<"\n Enter a string \n";
cin.getline(str,sizeof(str));
cout<<"\n Enter a character to be removed \n";
cin>>ch;
strlength = strlen(str);
for (i = 0; i < strlength; i++)
{
if (str[i] != ch)
{
new_word[k] = str[i];
k++;
}
}
cout<<"\n String without "<<ch<<" character : ";
for(i=0; i<k; i++)
{
cout<<new_word[i];
}
getch();
}

6.Write a program to search a given character in string.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int strlength = 0,i = 0,k = 0,count = 0,location[50];
char str[50],ch;
clrscr();
cout<<"\n Enter a string \n";
cin.getline(str,sizeof(str));
cout<<"\n Enter character to be searched \n";
cin>>ch;
strlength = strlen(str);
for (i = 0; i < strlength; i++)
{
if (str[i] == ch)
{
count++;
location[k] = i+1;
k++;
}
}
if (count > 0)
{
cout<<"\n"<<ch<<" is present "<<count<<" times at ";
for(i = 0; i < k; i++)
{
cout<<location[i]<<" ";
}
cout<<"positions";
}
else
{
cout<<"\n"<<ch<<" not found";
}
getch();
}

7.Write a program to sort a string array in descending order.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int n,i = 0,j = 0;
char str[10][50],*store;
clrscr();
cout<<"\n Enter size of string array \n";
cin>>n;
cout<<"\n Enter array string \n";
for(i = 0; i < n; i++)
{
cin>>str[i];
}
cout<<"\n Entered array strings : \n";
for(i = 0; i < n; i++)
{
cout<<str[i]<<"\n";
}
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(strcmp(str[i],str[j]) < 0)
{
strcpy(store,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],store);
}
}
}
cout<<"\n String array in descending order : \n";
for(i = 0; i < n; i++)
{
cout<<str[i]<<"\n";
}
getch();
}

8.Write a program to sort string array in ascending order.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int n,i = 0,j = 0;
char str[10][50],*store;
clrscr();
cout<<"\n Enter size of string array \n";
cin>>n;
cout<<"\n Enter array string \n";
for(i = 0; i < n; i++)
{
cin>>str[i];
}
cout<<"\n Entered array strings : \n";
for(i = 0; i < n; i++)
{
cout<<str[i]<<"\n";
}
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(strcmp(str[i],str[j]) > 0)
{
strcpy(store,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],store);
}
}
}
cout<<"\n String array in ascending order : \n";
for(i = 0; i < n; i++)
{
cout<<str[i]<<"\n";
}
getch();
}

Leave a Reply

Your email address will not be published. Required fields are marked *