0% found this document useful (0 votes)
31 views

Lab CS-201 Week # 4 Problem Statement: #Include #Include Using Namespace STD

The document describes a C++ program that compares two character arrays passed as strings. The CompareStrings function takes the two arrays and their size as arguments. It uses a while loop to check if each character is the same, and returns whether the strings are equal or not equal. The main function gets the strings from the user, checks their sizes are equal, and calls CompareStrings to compare the strings and output the result.

Uploaded by

Usman Haider
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Lab CS-201 Week # 4 Problem Statement: #Include #Include Using Namespace STD

The document describes a C++ program that compares two character arrays passed as strings. The CompareStrings function takes the two arrays and their size as arguments. It uses a while loop to check if each character is the same, and returns whether the strings are equal or not equal. The main function gets the strings from the user, checks their sizes are equal, and calls CompareStrings to compare the strings and output the result.

Uploaded by

Usman Haider
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab CS-201

Week # 4

Problem Statement

Write a program that will take


1. Two strings names as input from the user in the form of character arrays namely as
“firstArray” and “secondArray” respectively.
2. Both arrays along with the size will be passed to the function “CompareStrings”.
3. CompareStrings function will use pointer to receive arrays in function and then start
comparing both arrays by using while loop.
4. If all the characters of both these arrays are same then the message “Both strings are
same” should be displayed on the screen.

Note: For comparing both these arrays, the size should be same.

#include <iostream>

#include <cstring>

using namespace std;

// function definition

void CompareStrings(char *arr1, char *arr2, int size){

int check = 1;

int i = 0;

while(i<size){

if(arr1[i] != arr2[i]){

check = 0;

break;

}
i++;

if(check == 1)

cout<< "Both strings are same" <<endl;

else

cout<< "Both strings not same " <<endl;

int main() {

char firstArray[20], secondArray[20];

cout<< "Enter the first Name: ";

cin.getline(firstArray, sizeof(firstArray));

cout<< "Enter the second Name: ";

cin.getline(secondArray, sizeof(secondArray));

if(strlen(firstArray) == strlen(secondArray)){

int size = strlen(firstArray);

CompareStrings (firstArray, secondArray, size);

else {

cout<< "Size of both names are not same" <<endl;

}
system("pause");

You might also like