Most Commonly Asked Programs In C++ Language

Most Commonly Asked Programs In C++ Language


Mini Project :~ C Program To Perform Various Operations On Strings. [Such As : Find Length, Copy, Reverse, Palindrome, Concatenation, Sub-string]

Posted: 12 Jun 2013 10:21 PM PDT

This Post Contains A Mini Project :~ C Program To Perform Various Operations On Strings. [Such As : Find Length, Copy, Reverse, Palindrome, Concatenation, Sub-string] With Correct Source Code, Algorithm & Output. This Program Is Written, Compiled & Executed At Turbo C/C++3.0 Compiler & Will Help You To Understand The Concept Of 'String Operations', 'Character Arrays', 'Functions', 'Menu Driven Concept' From C++ Language. It Is A Well-Structured Program With Proper Comments Which Provides Step-By-Step Description Of Various Features Of The Language In A Simple & Easy-To-Understand Way.


# Note : You Can Simply Copy-Paste The Following Program Or Code Into Compiler For Direct Result.



Mini Project :~ C Program To Perform Various Operations On Strings. [Such As : Find Length, Copy, Reverse, Palindrome, Concatenation, Sub-string]

/* Declaration Of Header Files */
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
/* Function Declarations */
void Scan1(char *);
void Scan2(char * , char *);
void Leng (char *);
void Copy (char * , char *);
void Reve (char * , char *);
void Palindrome(char * , char *);
void Concat    (char * , char *);
void Subs (char Str1[] , char Str2[]);
/* Start Of Main Program */
void main()
{
    /* Declaration Of Local Variables Of 'main()' Function */
    int  Num = 0;
    char Str1[50] , Str2[50];
    clrscr();
    /* Printing 'Menu' On Screen/Console */
    printf(" \n :: Welcome To 'String Operation' Program :: \n ");
    do
    {
        printf(" \n Please Select One Of The Options From Following Menu : ");
        printf(" \n 1. Length Of Given String. \n 2. Copy   Of Given String. \n 3. Reverse The String. \n 4.        Check For Palindrome. \n 5. Concatenate The Strings. \n 6. To Find The Substring. \n 7. EXIT  From  PROGRAM.");
        printf(" \n Please Enter Your Option Here ::\t\a");
        scanf("%d" , &Num);
        switch(Num)
        {
            case 1: textcolor(RED);
                       clrscr();
                       printf(" \n :: Welcome To Length Function :: \n ");
                       Scan1( Str1 );  // Call To Function.
                       Leng ( Str1 );   // Call To Function.
                       break;

            case 2: textcolor(GREEN);
                       clrscr();
                       printf(" \n :: Welcome To Copy Function :: \n ");
                       Scan1( Str1 );
                       Copy ( Str1 , Str2 );  // Call To Function.
                       break;

            case 3: textcolor(YELLOW);
                       clrscr();
                       printf(" \n :: Welcome To Reverse Function :: \n ");
                       Scan1( Str1 );
                       Reve ( Str1 , Str2 );   // Call To Function.
                       break;

            case 4: textcolor(10);
                       clrscr();
                       printf(" \n :: Welcome To Palindrome Function :: \n ");
                       Scan1( Str1 );
                       Palindrome( Str1 , Str2 );  // Call To Function.
                       break;

            case 5: textcolor(12);
                       clrscr();
                       printf(" \n :: Welcome To Concatenation Function :: \n ");
                       Scan2( Str1 , Str2 );
                       Concat(Str1 , Str2);   // Call To Function.
                       break;

            case 6: textcolor(5);
                       clrscr();
                       printf(" \n :: Welcome To Substring Function :: \n ");
                       Scan2( Str1 , Str2 );
                       Subs ( Str1 , Str2 );   // Call To Function.
                       break;

            case 7: exit (0);  // Exit From Program.
                        break;

            default: printf(" \n Sorry!!!. Please Enter A Valid Choice. ");
        }
    }while( Num <= 7 );
    /* End Of 'Menu' */
    getch();
}

/* End Of Main Program */
void Scan1(char *Str1)   // Function Definition.
{
    printf(" \n Please Enter The String ::\t ");
    fflush( stdin );
    gets  ( Str1 );
}
void Scan2(char *Str1 , char *Str2)   // Function Definition.
{
    printf(" \n Please Enter String 1 ::\t ");
    fflush( stdin );
    gets  ( Str1 );
    printf(" \n Please Enter String 2 ::\t ");
    fflush( stdin );
    gets  ( Str2 );
}
void Leng( char *Str1 )   // Function Definition.
{
    int i = 0;
    while( Str1[i] != '\0' )
    {
        i++;
    }
    printf(" \n The Length Of String Is ::\t%d ", i);
    getch();
}
void Copy( char *Str1 , char *Str2 )  // Function Definition.
{
    int i = 0 , j = 0;
    while( Str1[i] != '\0' )
    {
        Str2[j] = Str1[i];
        j++;
        i++;
    }
    Str2[j] = '\0';
    printf("The Entered String Is ::\t");
    puts(Str1);
    printf("Copy Of Given String Is ::\t");
    puts(Str2);
    getch();
}
void Reve( char *Str1 , char *Str2 )    // Function Definition.
{
    int i = 0 , j = 0;
    while( Str1[i] != '\0' )
    {
        i++;
    }
    for(i--; i>=0; i--)
    {
        Str2[j] = Str1[i];
        j++;
    }
    Str2[j] = '\0';
    printf("The Entered String Is ::\t");
    puts(Str1);
    printf("Reversed Of Given String Is ::\t");
    puts(Str2);
    getch();
}
void Palindrome( char *Str1 , char *Str2 )   // Function Definition.
{
    int i = 0, j = 0, flag = 0;
    while( Str1[i] != '\0' )
    {
        i++;
    }
    for(i--; i>=0; i--)
    {
        Str2[j] = Str1[i];
        j++;
    }
    Str2[j] = '\0';
    i=0;
    j--;
    while( Str1[i] == Str1[j] )
    {
        flag = 1;
        i++;
        j--;
    }
    if( flag == 1 )
    {
        printf("\nThe Given String Is A Palindrome");
    }
    else
    {
        printf("\nThe Given String Is Not A Palindrome");
    }
    getch();
}
void Concat( char *Str1 , char *Str2 )   // Function Definition.
{
    int i = 0 , j = 0;
    while( Str1[i] != '\0' )
    {
        i++;
    }
    while( Str2[j] != '\0' )
    {
        Str1[i] = Str2[j];
        i++;
        j++;
    }
    Str1[i] = '\0';
    printf("\n The Concatenated String Is :");
    puts( Str1 );
    getch();
}
void Subs( char Str1[] , char Str2[] )   // Function Definition.
{
    int i , j , flag = 0;
    for(j=0; Str2[j] != '\0'; j++)
    {
        for(i=0; Str1[i] != '\0'; i++)
        {
            if( Str1[i] == Str2[j] )
            {
                flag = 1;
                break;
            }

        }
    }
    if( flag == 1 )
        {
            printf("\nThe Given String 2 Is A Substring Of String 1");
        }
        else
        {
            printf("\nThe Given String 2 Is Not A Substring Of String 1");
        }
    getch();
}


Post a Comment