Answer to Question #330140 in C++ for Ace

Question #330140
Assume your team is part of a research project. Your team wants to create a simple software that compares two strings, but they're undecided about whetheror not to use pointers. Your group now has the duty of creating or converting your initial code into a pointer-based compare two strings program and determining whether or not pointers are useful in this program.

Example like this:
#include <iostream>
using namespace std;
string convert(string s)
{
for (int i=0; i<s.length() ;i++)
{
 s[i]=toupper (s[i]);
}
return s;
}

int main()
{ 
string string01,string02;
cout<<"String 1 : ";
cin>>string01;
cout<<"String 2 : ";
cin>>string02;

if(convert(string01)==convert(string02))  {
cout<<"Two strings is equal";
}
else{
cout<<"Two strings is not equal";
}
}
1
Expert's answer
2022-04-18T08:40:03-0400
#include <iostream>

using namespace std;

char* convert(char* s)
{
	for (int i = 0; i<strlen(s); i++)
	{
		s[i] = toupper(s[i]);
	}
	return s;
}

int main()
{
	char string01[20];//pointers less efficient because we have to
	char string02[20];//allocate memory for chars in contrast of string
	cout << "String 1 : ";
	cin >> string01;
	cout << "String 2 : ";
	cin >> string02;


	if (strcmp(convert(string01),convert(string02))==0) {
		cout << "Two strings is equal";
	}
	else {
		cout << "Two strings is not equal";
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS