C++ Program to find substring in a given string [DEVCPP/GCC]


STRING FUNCTION: FIND

size_t  find  (const string& substring , size_t  pos=0 )

This function is used to search a string for a given substring specified in its arguments. If the substring is found, it returns the starting index of the substring else returns -1. The argument pos specifies the position from which the search will begin. The default value of pos is 0.

PROGRAM

// Program to find substring in a given string

#include <iostream>
#include <string>

using namespace std;

int main()
{
        string str,sub;
        int pos;

        cout<<"ENTER STRING: ";
        getline(cin,str);

        cout<<"ENTER SUBSTRING: ";
        cin>>sub;

        pos=str.find(sub,0);

         if(pos==-1)
        {
               cout<<"SUBSTRING NOT FOUND !";
        }
        else
        {
               cout<<"SUBSTRING FOUND AT POSITION: "<<pos;
        }

         return 0;
}

OUTPUT

C++ Program to find substring in a given string with output

C++ Program to perform multiplication of two matrices [DEVCPP/GCC]

PROGRAM

//Program to perform multiplication of two matrices

# include <iostream>

using namespace std;

int main()
{
int a[3][3],b[3][3],c[3][3],i,j,k,s;
 
  cout<<"ENTER FIRST  MATRIX ROW WISE:\n";
for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}
cout<<endl;
}

cout<<"ENTER SECOND MATRIX ROW WISE:\n";
  for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
cout<<"b["<<i<<"]["<<j<<"]=";
cin>>b[i][j];
}
cout<<endl;
}

//Loop For Multiplication

for(i=0 ; i<3 ; i++)
{
for(j=0 ; j<3 ; j++)
{
for(k=0 ; k<3 ; k++)
{
s = s + (a[i][k] * b[k][j]);
}
                  c[i][j]=s;
                  s=0;
}
}
 
  cout<<"\nRESULT\n";
for(i=0;i<3;i++)    
{
for(j=0;j<3;j++)
{
cout<<c[i][j]<<" ";
}
cout<<endl;
}
 
   return 0;

}

OUTPUT

C++ Program to perform multiplication of two matrices with output

C++ Program to count the number of spaces in a given string [DEVCPP/GCC]


PROGRAM

//Program to count the number of spaces in a given string

#include<iostream>

using namespace std;

int main()
{
        string s;
        int len,i,count=0;

        cout<<"ENTER STRING: ";
        getline(cin,s);

        len=s.size();

        for(i=0;i<len;i++)
       {
                if(s[i]==' ')
                {
                        count++;
                }
       }

        cout<<"NUMBER OF SPACES IN THE GIVEN STRING ARE "<<count;

        return 0;
}

OUTPUT

C++ Program to count the number of spaces in a given string with output

C++ Program to reverse the words of a given string [DEVCPP/GCC]

PROBLEM

We wish to reverse the words of a sentence/phrase without using another array.
For example, I AM GOOD should become GOOD AM I.

STEPS

We will solve the given problem in two steps:

1. We will reverse the whole string.
            DOOG MA I

2. We will reverse each word seperately.
            GOOD AM I

PROGRAM

// Program to reverse the words in a given string (without using another array)

#include <iostream>

using namespace std;

int main()
{
        string s;
        int begin,end,i,j=0,len,temp,count=0;

        cout<<"ENTER STRING: ";
        getline(cin,s);

        //To find the length of string
        len=s.length();

        //To reverse whole string
        for(i=0;i<(len/2);i++)
        {
                temp=s[i];
                s[i]=s[len-1-i];
                s[len-1-i]=temp;
        }

        //To reverse each word seperately
        for(i=0;i<len;i++)
        {
                 if(s[i]==' ' || s[i]=='\0')
                {
                         for(begin=j,end=i-1 ; begin<(i+j)/2 ; begin++,end--)
                         {
                                   temp=s[begin];
                                   s[begin]=s[end];
                                   s[end]=temp;
                         }
                         j=i+1;
                }
         }

       cout<<s<<" ";

       return 0;
}

OUTPUT


C++ Program to reverse the words of a given string with output

C++ Program to sort elements using Selection Sort [DEVCPP/GCC]

SELECTION SORT

Selection sort is conceptually the most simplest sorting algorithm. It is named as selection sort because at each iteration an index is selected and at the end of that iteration, elements till that index are sorted.

There are two ways of performing selection sort:

1.This algorithm finds the maximum value in the array and exchanges it with the element in the last position, then finds the second largest element and exchanges it with the second last position, and continues in this way until the array is sorted.

2. This algorithm finds the minimum value in the array and exchanges it with the element in the first position, then finds the second smallest value and exchanges it with the second position and continues in this way until the array is sorted.

Here we go with the 2nd way...

EXAMPLE

C++ Program to sort elements using Selection Sort



C++ Program to sort elements using Selection Sort


C++ Program to sort elements using Selection Sort


C++ Program to sort elements using Selection Sort


C++ Program to sort elements using Selection Sort

STEPS

1. Input the elements of an array.

2. Pass the array and its length to the function Insertion_Sort(int A , int N )

3. Intialize i with 0 and assign i to mid_index.

4.Initialize j with (i+1).

5.Compare the value at jth index with the value at min_index , update min_index if the condition
  A[j]<A[mid_index]  is true, with the lower value index.

6. Increment j as j+1;

7. Repeat steps (5-6) till j<N , where N is the length of an array.

8. If  min_index is not equal to i, exchange the value at min_index with the value at ith index.

9. Increment i as i+i;

10. Repeat steps  (8-9) till i<N.

PROGRAM

//Program demonstrating Selection Sort 

# include <iostream>

using namespace std;

void selection_sort(int A[],int N)
{
int i,j,min_index,temp;

for(i=0;i<N;i++)
{
min_index = i;

for( j=(i+1);j<N;j++ )  //Loop to find the smallest element in the remaining array.
{
if(A[j]<A[min_index])
{
min_index = j;
}
}

if(min_index != i)
{
temp = A[min_index];
A[min_index] = A[i];
A[i] = temp;
}
}

cout<<"SORTED ARRAY:"<<endl;
for(i=0;i<N;i++)
{
cout<<"A["<<i<<"]: "<<A[i]<<endl;
}
}

int main()
{
int Arr[5],i;
for(i=0;i<5;i++) //Loop to input the elements of an array(unsorted basically)
{
cin>>Arr[i];
}

selection_sort(Arr,5);
return 0;
}

OUTPUT

C++ Program to sort elements using Selection Sort with output

C++ Program to sort elements using Bubble Sort [DEVCPP/GCC]

BUBBLE SORT

Bubble Sort is a sorting technique which is used to sort elements of an array. This technique involves multiple comparison of adjacent elements in single iteration.

This technique is named as bubble sort because the lighter elements are shifted "upwards" just as bubbles rise upwards. At the end of each iteration the heaviest element gets in its correct position.

EXAMPLE

C++ Program to sort elements using Bubble Sort

C++ Program to sort elements using Bubble Sort

C++ Program to sort elements using Bubble Sort

C++ Program to sort elements using Bubble Sort

C++ Program to sort elements using Bubble Sort


STEPS

1. Input the elements of an array.

2. Pass the array and its length to the function Bubble_Sort(int list , int length )

3. Initialize i and j with 0.

4. Initialize a boolean variable (flag) with true.

5. Compare the jth element with(j+1)th element. If the value at list(j) is greater then list(j+1) then exchange jth elemet and (j+1)th element.
 
6. Increment j as j=j+1.

7. Repeat steps 4-5 till j<(length-i-1).

8. Check flag, if it remains true then the list is sorted and no further comparisons are required.

9. Increment i as i=i+1.

10. Repeat steps 4-7 till i<(length-1).

PROGRAM

// Program to sort elements in ascending order using Bubble Sort

#include <iostream>

using namespace std;

void Bubble_Sort( int list[], int length )
{
        int i,j,temp;
        bool flag;

        for(i=0 ; i<(length-1) ; i++)
        {
                flag=true;

                for(j=0 ; j<((length-1)-i) ; j++)
                {
                        if( list[j] > list [j+1] )
                       {
                                flag=false;
                                temp = list[j];
                                list[j] = list [j+1];
                                list[j+1] = temp;
                       }
                }

                if(flag)
               {
                         break;
               }
         }

cout<<"\n SORTED ARRAY: \n";

for(i=0 ; i<length ; i++ )
        {
                   cout<<"\nA["<<i<<"]: "<<list[i];
         }
}

int main()
{
        int arr[10],i;

        cout<<"\nENTER LIST OF NUMBERS TO SORT: \n";

        for(i=0 ;i< 10 ; i++ )
        {
                cout<<"A["<<i<<"]: ";
                cin>>arr[i];
        }

        Bubble_Sort(arr,10);

        return 0;
}

TIME COMPLEXITY

BEST CASE: When the List is already sorted.
COMPLEXITY: O(n)

AVERAGE CASE: When the List is not sorted.
COMPLEXITY: O(n^2)

WORST CASE: When the List is already sorted in reverse order.
COMPLEXITY: O(n^2)

OUTPUT

C++ Program to sort elements using Bubble Sort

C++ Program to check the equality of two matrices (2 dimensional array) [DEVCPP/GCC]

PROGRAM

//Program to check the equality of two matrices

# include <iostream>

using namespace std;

int main()
{

int  flag=0,a[3][3],b[3][3],i,j;

cout<<"ENTER FIRST MATRIX ROW WISE:\n";

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}
cout<<endl;
}

cout<<"ENTER SECOND MATRIX ROW WISE:\n";

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<"b["<<i<<"]["<<j<<"]=";
cin>>b[i][j];
}
cout<<endl;
}


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]!= b[i][j]) //checking for equality
{
flag==1;
break;
}
}
}

if(flag)
cout<<"MATRICES ARE UNEQUAL";
else
cout<<"MATRICES ARE EQUAL";

return 0;
}

OUTPUT

C++ Program to check the equality of two matrices (2 dimensional array) with output

C++ Program to calculate frequency of a character in a given string [DEVCPP/GCC]

FREQUENCY

Frequency of a character in a given string implies no of occurences of that particular character in the given string.

For example:

String:        Techcpp
Character:  c
Frequency: 2


STEPS

1. Initialize count with 0.

2. Input an string and the character whose frequency is to be calculated within the entered string.

3. Initialize index variable i with 0

4. Increment the count if the condition 'ch equals to a[i]' is satisfied.

5. Increment i untill a[i]!='\0'. 

PROGRAM

//Program to calculate the frequency of a character

# include <iostream>

using namespace std;

int main()
{
char a[20],ch;
int i,j,count=0;

cout<<"ENTER THE STRING:\n";
cin.getline(a,20);

cout<<"ENTER THE CHARACTER WHOSE FREQUENCY IS TO BE CALCULATED:\n";
cin>>ch;

for(i=0;a[i]!='\0';i++)
{
if(ch==a[i])
count++;
}

cout<<count;
return 0;
}

OUTPUT

C++ Program to calculate frequency of a character in a given string with output

C++ Program to check whether a number is palindrome or not [DEVCPP/GCC]


PALINDROME NUMBER

A number is called palindrome if it remains same after reversing its digits.
For example,
Reverse(2002) = 2002 i.e. Palindrome Number
Reverse(2111) = 1112 i.e. Not a Palindrome Number

STEPS

1. Input a number(num).

2. Initialize another variable(original) with num. It would be used to compare the reversed number.

3. Obtain the last digit of num.

4.  Assign rev(Initially 0) with (rev*10) + lastnum.

5.  Decrement num as num/10.

6.  Repeat steps 3-5 until num>0.

7.  Compare Reversed Number(rev) with Original Number(original). If they are same, the given number is palindrome else not.

PROGRAM

//Program to check whether a number is palindrome or not

#include<iostream>

using namespace std;

int main()
{
int num, rev=0, original, lastnum;

cout<<"ENTER A NUMBER: ";
cin>>num;

original=num;

while(num>0)
{
                lastnum=num%10;
rev=(rev*10) + lastnum;
num=num/10;
}

if(original==rev)
{
cout<<"GIVEN NUMBER "<<original<<" IS A PALINDROME";
}
else
{
cout<<"GIVEN NUMBER "<<original<<" IS NOT A PALINDROME";
}

return 0;
}

OUTPUT

C++ Program to check whether a number is palindrome or not with output

FIND US ON FACEBOOK!