Search This Blog

Saturday, 12 May 2018

Introduction to Programming (CS201)-Lab Exercise No.3 - WEEK 5 (comparing the strings)

#include <iostream>

using namespace std; //Allows computer to output data

//declare functions
int CompareStrings(char[], char[]); //for comparing array of strings
int sLength ( char[] );  //for calculate the length of string

main() //main function begins
{
 //declare variables
int firstsize=0,secondsize=0,f=0;
  char first[100]={};
  char second[100]={};

 //get input from keyboard
  cout << "Enter the first Name ";
  gets(first);

 //call function
  firstsize=sLength(first);
 
 //get input from keyboard
  cout << "Enter the second Name ";
  gets(second);

 //call function
  secondsize=sLength(second);

 //call function
  f=CompareStrings(first, second);

 //check the size of strings and strings are same or not
  if (firstsize!=secondsize)
  {
  cout << "Size of Both names are not same";
  }
  else if (f==0)
  {
  cout << "Both strings are  same";
  }
  else if(f==1)
  {
  cout << "Both strings are not same";
  }

}//end of main function


//function which compare the two arrays of string
int CompareStrings(char firstArray[100], char secondArray[100])
{
int i;
int flag=0;

while (i<100)
{
if ((sLength(firstArray)==sLength(secondArray)) && (firstArray[i]!=secondArray[i]))
{
flag=1;
break;
}
else if (firstArray[i]==secondArray[i])
{
flag=0;
}
i++;
}
return flag;
}


int sLength ( char str[100] )
{
int i,count=1;


    for(i=0;str[i]!='\0';i++)
    {
    count++;
    }
    return count-1;
}

2 comments:

  1. Dear sir cs201 Assignment NO 2 chhiya.

    ReplyDelete
    Replies
    1. Please down load it
      https://vumcssolutions.blogspot.com/2018/05/introduction-to-programming-cs201_22.html

      Delete