Tuesday, November 21, 2017

C++ Code Review: Using pointer to compare two strings for equality.

Description: This program uses pointer to compare two strings for equality.

Code:

//Compiling Tool: Code::Block IDE with MinGW

//Program uses pointer to compare two strings for equality.

 

#include<iostream>

#include<conio.h> //for getch()

#include<cstdlib> //for system()

#include<stdio.h>

 

using namespace std;

int main()

{

 

    system("cls");//to clear the screen

 

    char str1[50],str2[50];

    int str_cmp(char*,char*);

 

    cout<<"Enter first string:";

    gets(str1);

    cout<<"Enter second string:";

    gets(str2);

 

    if(str_cmp(str1,str2))

        cout<<"Strings are equal";

    else

        cout<<"Strings are not equal";

 

    return 0;

}

 

 

int str_cmp(char *s1,char *s2)

{

    while(*s1==*s2)

    {

        if(*s1==' '||*s2==' ')

            break;

 

 

        s1++;

        s2++;

    }

 

 

    if(*s1==' '&&*s2==' ')

        return 1;

 

 

    return 0;

}       

 

Output:

clip_image002

If you want to write or construct or program C++ mini-project and do not know how or from where to start buy this simple e-book: Code Review of 26 C++ mini-projects. For book sample click this link.

Add your comments in below available comment box.

Note: Click these label/tags to view all related posts. Tags: C++Code

 

…till next post, bye-bye & take care.

No comments:

Post a Comment