C++ Programs Examples With Output [2021
CPP Examples]
July 28, 2017 admin
This post include list of basic/simple C++(CPP) programming logic examples with
[Link] is a basic small and simple C programs tutorial for beginner to help for
practicing and learn for [Link] post include list of C++ programs,explanation
with output.
This post also include basic C++ programs examples PDF if you want than download
C++ programming examples with output PDF from download section or Click here.
Numeric Array Programs
C++ provides a data structure such as Array, Array is a collection of data of same types
stored in sequential memory location. Array reduce length of code in some extend. The
elements in an array is accessed using an index .This section contains C++
programming examples on Arrays with explanation.
List of all C++ program based on numeric array are listed below.
[hide]
● [Link] a program to convert the given decimal number in to binary number.
● [Link] a program to delete an element from array.
● [Link] a program to insert an element in array.
● [Link] a program to remove consecutive duplicates from an array.
● [Link] a program to search an element in an array.
● [Link] a program to print array without duplicate numbers.
● [Link] a program to find series of factorial number.
● [Link] a program to sort an array in descending order.
● [Link] a program to sort an array in ascending order.
[Link] a program to convert the given decimal number in to binary number.
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int number=0,i=0,Binary[MAXSIZE],index=0;
clrscr();
cout<<"\n Enter the decimal number you want to convert : ";
cin>>number;
while (number > 0)
{
Binary[index] = number % 2;
number = number / 2;
index++;
}
cout<<"\n Binary of given decimal number is : ";
for (i = index - 1; i >= 0; i--)
{
cout<<Binary[i];
}
getch();
}
[Link] a program to delete an element from array.
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int n,a[MAXSIZE],i=0,k=0,position;
clrscr();
cout<<"\n Enter size of array \n";
cin>>n;
cout<<"\n Enter array elements \n";
for (i = 0; i < n; i++) {
cin>>a[i];
}
cout<<"\n\n Array elements are : \n";
for (i = 0; i < n; i++) {
cout<<a[i]<<"\t";
}
cout<<"\n\n Enter the position of element to be deleted \n";
cin>>position;
position = position - 1;
for (i = 0; i < n; i++) {
if (i != position) {
a[k] = a[i];
k++;
}
}
cout<<"\n Array elements are : \n";
for (i = 0; i < k; i++) {
cout<<a[i]<<"\t";
}
getch();
}
[Link] a program to insert an element in array.
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int n,a[MAXSIZE],i=0,position,element;
clrscr();
cout<<"\n Enter size of array \n";
cin>>n;
cout<<"\n Enter array elements \n";
for (i = 0; i < n; i++)
{
cin>>a[i];
}
cout<<"\n\n Array elements are : \n";
for (i = 0; i < n; i++)
{
cout<<a[i]<<"\t";
}
cout<<"\n\n Enter the element to be inserted ";
cin>>element;
cout<<"\n\n Enter the position where "<<element<<" is to be
inserted ";
cin>>position;
position = position - 1;
for (i = n - 1; i >= position; i--)
{
a[i + 1] = a[i];
}
a[position] = element;
cout<<"\n Array elements are : \n";
for (i = 0; i < n + 1; i++)
{
cout<<a[i]<<"\t";
}
getch();
}
[Link] a program to remove consecutive duplicates from an array.
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int n,a[MAXSIZE],i=0,k=1,element;
clrscr();
cout<<"\n Enter size of array \n";
cin>>n;
cout<<"\n Enter array elements \n";
for (i = 0; i < n; i++)
{
cin>>a[i];
}
element = a[0];
for (i = 1; i < n; i++) {
if (a[i] != element) {
a[k] = a[i];
element = a[i];
k++;
}
}
cout<<"\n Array without consecutive duplicate \n";
for (i = 0; i < k; i++) {
cout<<a[i]<<"\t";
}
cout<<"\n";
getch();
}
[Link] a program to search an element in an array.
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int n,a[MAXSIZE],location[MAXSIZE],i=0,k=0,ele;
clrscr();
cout<<"\n Enter size of array \n";
cin>>n;
cout<<"\n Enter array elements \n";
for (i = 0; i < n; i++)
{
cin>>a[i];
}
cout<<"\n Enter an element to be searched ";
cin>>ele;
for (i = 0; i < n; i++)
{
if (a[i] == ele)
{
location[k] = (i + 1);
k++;
}
}
if (k > 0) {
cout<<"\n Element "<<ele<<" is present at positions :";
for(i = 0; i < k; i++)
{
if(i < k-1) {
cout<<location[i]<<",";
}
else
{
cout<<location[i];
}
}
}
else {
cout<<"\n Element "<<ele<<" not found";
}
getch();
}
[Link] a program to print array without duplicate numbers.
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int n,a[MAXSIZE],b[MAXSIZE],i=0,j=0,k=0,flag=1;
clrscr();
cout<<"\n Enter size of array \n";
cin>>n;
cout<<"\n Enter array elements \n";
for (i = 0; i < n; i++)
{
cin>>a[i];
}
for (i = 0; i < n; i++) {
for (j = 0; j < k; j++) {
if (a[i] == b[j]) {
flag = 0;
break;
}
else {
flag = 1;
}
}
if (flag == 1) {
b[k] = a[i];
k++;
}
}
cout<<"\n Array without duplicates : \n";
for (i = 0; i < k; i++) {
cout<<b[i]<<"\t";
}
cout<<"\n";
getch();
}
[Link] a program to find series of factorial number.
#include<iostream.h>
#include<conio.h>
#define MAX 500
int multiply(int x, int res[], int res_size);
void factorial(int n)
{
int res[MAX],i=0,x=0,res_size = 1;;
res[0] = 1;
for (x=2; x<=n; x++)
{
res_size = multiply(x, res, res_size);
}
for (i=res_size-1; i>=0; i--)
{
cout<<res[i];
}
}
int multiply(int x, int res[], int res_size)
{
int carry = 0,i=0,prod;
for (i=0; i<res_size; i++)
{
prod = res[i] * x + carry;
res[i] = prod % 10;
carry = prod/10;
}
while (carry)
{
res[res_size] = carry%10;
carry = carry/10;
res_size++;
}
return res_size;
}
void main()
{
int n,i=0;
clrscr();
cout<<"\n Enter a number to which you want to print factorials
of every number : ";
cin>>n;
cout<<"Factorial of every number till given number is \n";
for(i=1; i<=n; i++)
{
factorial(i);
cout<<"\n";
}
getch();
}
[Link] a program to sort an array in descending order.
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int n,i=0,j=0,a[MAXSIZE],t;
clrscr();
cout<<"\n Enter size of array \n";
cin>>n;
cout<<"\n Enter array elements \n";
for (i = 0; i < n; i++) {
cin>>a[i];
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (a[i] < a[j]) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
cout<<"\n Array elements in descending order: \n";
for (i = 0; i < n; i++) {
cout<<a[i]<<"\t";
}
getch();
}
[Link] a program to sort an array in ascending order.
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int n,i=0,j=0,a[MAXSIZE],t;
clrscr();
cout<<"\n Enter size of array \n";
cin>>n;
cout<<"\n Enter array elements \n";
for (i = 0; i < n; i++) {
cin>>a[i];
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
cout<<"\n Array elements in ascending order: \n";
for (i = 0; i < n; i++) {
cout<<a[i]<<"\t";
}
getch();
PAGE 2
Numeric Value Programs
In this section, we will see basic programs which we should know to code and also to
[Link] value programs provide a way to play with number without any
[Link] you think logically than you can solve any problem related to numeric value
[Link] of all C++ program based on numeric value are listed below.
[Link] a program to print reverse of a number.
#include<iostream.h>
#include<conio.h>
void main()
{
int n,value=0,rev=0,d=0;
clrscr();
cout<<"\n Enter a number \n";
cin>>n;
value = n;
while (n > 0) {
d = n % 10;
rev = (rev * 10) + d;
n = n / 10;
}
cout<<"\n The reverse of "<<value<<"is "<<rev;
getch();
}
[Link] a program to check whether a number is even or odd.
#include<iostream.h>
#include<conio.h>
void main()
{
int n;
clrscr();
cout<<"\n Enter a number \n";
cin>>n;
if (n % 2 == 0) {
cout<<n<<" is even";
}
else {
cout<<n<<" is odd";
}
getch();
}
3. Write a program to print factorial of a number.
#include<iostream.h>
#include<conio.h>
void main()
{
int n,fact=1,i=0;
clrscr();
cout<<"\n Enter a number \n";
cin>>n;
for (i = 1; i <= n; i++) {
fact = fact * i;
}
cout<<"\n Factorial of "<<n<<"is "<<fact;
getch();
}
[Link] a program to check whether a number is prime or not.
#include<iostream.h>
#include<conio.h>
void main()
{
int n,flag=0,i=0;
clrscr();
cout<<"\n Enter a number \n";
cin>>n;
for (i = 2; i < n; i++) {
if (n % i == 0) {
flag = 0;
break;
} else {
flag = 1;
}
}
if (flag == 0) {
cout<<"\n"<<n<<" is not prime number";
}
else {
cout<<"\n"<<n<<" is a prime number";
}
getch();
}
5. Write a program to check the given number is either palindrome or not.
#include<iostream.h>
#include<conio.h>
void main()
{
int n,value=0,rev=0,d=0;
clrscr();
cout<<"\n Enter a number \n";
cin>>n;
value = n;
while (n > 0) {
d = n % 10;
rev = (rev * 10) + d;
n = n / 10;
}
if (rev == value) {
cout<<"\n"<<value<<" is a palindrome";
}
else {
cout<<"\n"<<value<<" is not a palindrome";
}
getch();
}
[Link] a program for x raise y.
#include<iostream.h>
#include<conio.h>
void main()
{
int x,y,value=1,i=0;
clrscr();
cout<<"\n Enter x ";
cin>>x;
cout<<"\n Enter y ";
cin>>y;
for (i = 0; i < y; i++) {
value = value * x;
}
cout<<"\n x^y "<<x<<"^"<<y<<" is "<<value;
getch();
}
7. Write a program to swap two numbers.
#include<iostream.h>
#include<conio.h>
void main()
{
int x,y,t=0;
clrscr();
cout<<"\n Enter x ";
cin>>x;
cout<<"\n Enter y ";
cin>>y;
cout<<"\n Before swapping x="<<x<<" and y="<<y;
t = x;
x = y;
y = t;
cout<<"\n After swapping x="<<x<<" and y="<<y;
getch();
}
8. Write a program to calculate a power of 2 without using any function.
#include<iostream.h>
#include<conio.h>
void main()
{
int number,i,answer = 2;
clrscr();
cout<<"\n Enter the number:";
cin>>number;
for (i = 1; i < number; i++) {
answer = 2 * answer;
}
cout<<"Value of given power is "<<answer;
getch();
}
[Link] a program to find LCM of number.
#include<iostream.h>
#include<conio.h>
void main()
{
int lcm = 1, a, b,i=0;
clrscr();
cout<<"\n Enter the 1st number : ";
cin>>a;
cout<<"\n Enter the 2nd number : ";
cin>>b;
for (i = a; i <= a * b; i++) {
if (i % a == 0 && i % b == 0) {
lcm = i;
break;
}
}
cout<<"\n L.C.M.="<<lcm;
getch();
}
10. Write a program to swap two numbers without using third variable.
#include<iostream.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
cout<<"\n Enter x ";
cin>>x;
cout<<"\n Enter y ";
cin>>y;
cout<<"\n Before swapping x="<<x<<" and y="<<y;
x = x + y;
y = x - y;
x = x - y;
cout<<"\n After swapping x="<<x<<" and y="<<y;
getch();
}
[Link] a program to print 1 to n without using loop.
#include<iostream.h>
#include<conio.h>
void print(int n);
void main()
{
int n;
clrscr();
cout<<"\n Print numbers till ? ";
cin>>n;
cout<<"\n";
print(n);
getch();
}
voidprint(int n) {
if (n >= 1) {
print(n - 1);
cout<<n<<"\n";
}
}
PAGE 3
Pattern Programs
Pattern is nothing but series of any character either it is series of number or any
characters that are used to create some pattern or geometrical shape.
Pattern programs are one of the test practicable programs which increases our
efficiency to code for graphics patterns.
Pattern programs enhance your logical thinking abilities by [Link] is a list of
basic pattern programs which will be useful for you.
[Link] a program to print the following patterns.
****
***
**
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int n,i=0,j=0;
clrscr();
cout<<"\n How many rows you want in this pattern \n";
cin>>n;
cout<<"\n";
for (i = 0; i < n; i++)
{
for (j = n; j > i; j--)
{
cout<<"*";
}
cout<<"\n";
}
getch();
}
[Link] a program to print the following patterns.
**
***
****
#include<iostream.h>
#include<conio.h>
void main()
{
int n,i=0,j=0,k=0;
clrscr();
cout<<"\n How many rows you want in this pattern \n";
cin>>n;
cout<<"\n";
for (i = 0; i < n; i++) {
for (j = n - 1; j > i; j--) {
cout<<" ";
}
for (k = 0; k <= i; k++) {
cout<<"*";
}
cout<<"\n";
}
getch();
}
[Link] a program to print the following patterns.
#include<iostream.h>
#include<conio.h>
void main()
{
int n,i=0,j=0,k=0,count=1;
clrscr();
cout<<"\n How many rows you want in this pattern \n";
cin>>n;
cout<<"\n";
for (i = 0; i < n; i++)
{
for (j = n - 1; j > i; j--)
{
cout<<" ";
}
for (k = 0; k < count; k++)
{
cout<<"*";
}
count = count + 2;
cout<<"\n";
}
getch();
}
[Link] a program to print the following patterns.
#include<iostream.h>
#include<conio.h>
void main()
{
int n,i=0,j=0,k=0;
clrscr();
cout<<"\n How many rows you want in this pattern \n";
cin>>n;
cout<<"\n";
for (i = 0; i < n; i++)
{
for (j = 0; j < i; j++)
{
cout<<" ";
}
for (k = n; k > i; k--)
{
cout<<"*";
}
cout<<"\n";
}
getch();
}
[Link] a program to print the following patterns.
#include<iostream.h>
#include<conio.h>
void main ()
{
int n,i=0,j=0;
clrscr();
cout<<"\n How many rows you want in this pattern \n";
cin>>n;
cout<<"\n";
for (i = 0; i < n; i++)
{
for (j = 0; j <= i; j++)
{
cout<<"*";
}
cout<<"\n";
}
getch();
}
PAGE 4
Series Programs
In this section, we are going to show different series [Link] programs that are
in sequential manner to Nth [Link] practice with such programs increases our
ability to solve different kind of series puzzles. This post has a number of various java
programs with simple logic based on series along with its output.
As series questions are often asked in aptitude test where we have to put our logic and
calculate the next [Link] types of series program are explain in given example.
[Link] a program to print following series.
0 1 2 3 6 11
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 50
void main()
{
int n;
int a[MAXSIZE];
int k = 3, count = 0,i=0,j=0,next=0;
clrscr();
cout<<"\n Enter number of terms in series \n";
cin>>n;
a[0] = 0;
a[1] = 1;
a[2] = 2;
for (i = k; i < n; i++) {
next = 0;
for (j = count; j < k; j++) {
next = next + a[j];
}
count++;
a[k] = next;
k++;
}
cout<<"\n";
for (i = 0; i < n; i++) {
cout<<a[i]<<"\t";
}
getch();
}
[Link] a program to print following series.
2 4 6 8 10 12
#include<iostream.h>
#include<conio.h>
void main()
{
int n,count=0,i=0;
clrscr();
cout<<"\n Enter number of terms in series \n";
cin>>n;
cout<<"\n";
for (i = 0; i < n; i++) {
count = count + 2;
cout<<count<<"\t";
}
getch();
}
[Link] a program to print following series.
1 3 5 7 9 11
#include<iostream.h>
#include<conio.h>
void main()
{
int n,count=1,i=0;
clrscr();
cout<<"\n Enter number of terms in series \n";
cin>>n;
for (i = 0; i < n; i++) {
cout<<count<<"\t";
count = count + 2;
}
getch();
}
[Link] a program to print following series.
2 -4 6 -8 10 -12
#include<iostream.h>
#include<conio.h>
void main()
{
int n,positive = 2, negative = -4,i=0;
clrscr();
cout<<"\n Enter number of terms in series \n";
cin>>n;
cout<<"\n";
for (i = 0; i < n; i++) {
if (i < n) {
cout<<positive<<"\t";
positive = positive + 4;
}
i++;
if (i < n) {
cout<<negative<<"\t";
negative = negative - 4;
}
}
getch();
}
[Link] a program to print following series.
1 -3 5 -7 9 -11
#include<iostream.h>
#include<conio.h>
void main()
{
int n,positive = 1,negative = -3,i=0;
clrscr();
cout<<"\n Enter number of terms in series \n";
cin>>n;
for (i = 0; i < n; i++) {
if (i < n) {
cout<<positive<<"\t";
positive = positive + 4;
}
i++;
if (i < n) {
cout<<negative<<"\t";
negative = negative - 4;
}
}
getch();
}
[Link] a program to print following series.
4 9 16 25 36
#include<iostream.h>
#include<conio.h>
void main()
{
int n,square=0,i=0;
clrscr();
cout<<"\n Enter number of terms in series \n";
cin>>n;
cout<<"\n";
for (i = 1; i <= n; i++) {
square = i * i;
cout<<square<<"\t";
}
getch();
}
[Link] a program to print following series.
1 8 27 64 125
#include<iostream.h>
#include<conio.h>
void main()
{
int n,cube=0,i=0;
clrscr();
cout<<"\n Enter number of terms in series \n";
cin>>n;
cout<<"\n";
for (i = 1; i <= n; i++) {
cube = i * i * i;
cout<<cube<<"\t";
}
getch();
}
[Link] a program to print following series.
1/1 1/2 1/3 1/4 1/5
#include<iostream.h>
#include<conio.h>
void main()
{
int n,i=0;
clrscr();
cout<<"\n Enter number of terms in series \n";
cin>>n;
for(i = 1; i <= n; i++) {
cout<<"1/"<<i<<"\t";
}
getch();
}
[Link] a program to print following series.
1/1 -1/2 1/3 -1/4 1/5
#include<iostream.h>
#include<conio.h>
void main()
{
int n,i=0;
clrscr();
cout<<"\n Enter number of terms in series \n";
cin>>n;
for (i = 1; i <= n; i++) {
if (i % 2 == 0) {
cout<<"-1/"<<i<<"\t";
} else {
cout<<"1/"<<i<<"\t";
}
}
getch();
}
[Link] a program to print following series.
1/1! 1/2! 1/3! 1/4! 1/5! 1/6!
#include<iostream.h>
#include<conio.h>
long long int factorial(int N)
{
long long ans = 1;
int j;
for(j=1; j <= N; j++)
ans *= j;
return ans;
}
void main()
{
int n,i=0;
clrscr();
cout<<"\n Enter the number of terms \n";
cin>>n;
for(i=1; i<=n; i++)
{
cout<<factorial(i)<<"\t";
}
getch();
}
[Link] a program to print following series.
3 9 27 81 243
#include<iostream.h>
#include<conio.h>
void main()
{
int n,multiply = 1,i=0;
clrscr();
cout<<"\n Enter number of terms in series \n";
cin>>n;
cout<<"\n";
for (i = 1; i <= n; i++) {
cout<<multiply<<"\t";
multiply = multiply * 3;
}
getch();
}
[Link] a program to print following series.
314253
Method-1
#include<iostream.h>
#include<conio.h>
void main()
{
int n,count1=0,count2=0,i=0;
clrscr();
cout<<"\n Enter number of terms in series \n";
cin>>n;
cout<<"\n";
for (i = 0; i < n; i++) {
if (i % 2 == 0) {
cout<<count1<<"\t";
count1++;
} else {
cout<<count2<<"\t";
count2++;
}
}
getch();
}
Method-2
#include<iostream.h>
#include<conio.h>
void main()
{
int n,prev=0,i=0;
clrscr();
cout<<"\n Enter number of terms in series \n";
cin>>n;
cout<<"\n";
for (i = 0; i < n; i++) {
if (i % 2 == 0) {
cout<<prev<<"\t";
prev = prev + 3;
} else {
cout<<prev<<"\t";
prev = prev - 2;
}
}
getch();
}
[Link] a program to print following series.
0 1 2 3 6 12
#include<iostream.h>
#include<conio.h>
#define MAXSIZE 100
void main()
{
int n,a[MAXSIZE],i=0,j=0,k=3,next=0;
clrscr();
cout<<"\n Enter number of terms in series \n";
cin>>n;
a[0] = 0;
a[1] = 1;
a[2] = 2;
for (i = k; i < n; i++) {
next = 0;
for (j = 0; j < k; j++) {
next = next + a[j];
}
a[k] = next;
k++;
}
cout<<"\n";
for (i = 0; i < n; i++) {
cout<<a[i]<<"\t";
}
getch();
}
PAGE 5
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 [Link] 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 [Link] string ans string array examples with some
explanation given below describes more on string functionality.
[Link] 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();
}
[Link] 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";
[Link](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();
}
[Link] 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";
[Link](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();
}
[Link] 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";
[Link](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();
}
[Link] 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";
[Link](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();
}
[Link] 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";
[Link](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();
}
[Link] 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();
}
[Link] 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();
}