Question
Hi, I need help with my C++ code. I need to put everything in functions not in main. Also, I need help fixing step 3
Hi, I need help with my C++ code. I need to put everything in functions not in main. Also, I need help fixing step 3 to just one loop but it needs to contain if and else, return 0 and return 1. Thanks
This is my code:
#include
using std::cout;
using std::cin;
using std::endl;
//function templates
int myStrLen(const char*);
void myStrCpy(char*, const char*);
int myStrCmp(const char*, const char*);
void myStrSwap(char*, char*);
void myStrUpr(char*);
void myStrLwr(char*);
int main()
{
//STEP 1
char step1[] = { "Star" };
cout << step1 << endl;
cout << "Length = " << myStrLen(step1) << endl << endl;
//STEP 2
char step2a[] = { "This" };
char step2b[] = { "That" };
cout << "step2a = " << step2a << endl;
cout << "step2b = " << step2b << endl;
myStrCpy(step2a, step2b);
cout << "step2a = " << step2a << endl;
cout << "step2b = " << step2b << endl << endl;
//STEP 3
char step3a[] = { "Those" };
char step3b[] = { "These" };
cout << "step3a = " << step3a << endl;
cout << "step3b = " << step3b << endl;
if (myStrCmp(step3a, step3b) == 0)
cout << "They are the same!" << endl << endl;
else
cout << "They are NOT the same!" << endl << endl;
//STEP 4
char step4a[] = { "Them" };
char step4b[] = { "They" };
cout << "step4a = " << step4a << endl;
cout << "step4b = " << step4b << endl;
myStrSwap(step4a, step4b);
cout << "step4a = " << step4a << endl;
cout << "step4b = " << step4b << endl << endl;
//STEP 5
char step5[] = { "toupper" };
cout << step5 << endl;
myStrUpr(step5);
cout << step5 << endl << endl;
//STEP 6
char step6[] = { "TOLOWER" };
cout << step6 << endl;
myStrLwr(step6);
cout << step6 << endl << endl;
system("pause");
}
//STEP 1 of 7: C - String Length Function
int myStrLen(const char* str)
{
int len = 0;
for (int i = 0; str[i] != '\0'; i++)
{
len += 1;
}
return len;
}
//STEP 2 of 7: C - String Copy Function
void myStrCpy(char* dest, const char* src)
{
int srcLen = 0;
for (int i = 0; src[i] != '\0'; i++)
{
dest[i] = src[i];
srcLen += 1;
}
dest[srcLen] = '\0';
}
//STEP 3 of 7: Write A C - String Compare Function
int myStrCmp(const char* str1, const char* str2)
{
int i = 0;
for (i = 0; str1[i] != '\0' && str2[i] != '\0'; i++)
{
if (str1[i] != str2[i])
{
return 1;
}
}
if (str1[i] == '\0' && str2[i] == '\0')
{
return 0;
}
else
{
return 1;
}
}
//STEP 4 of 7: Write A C-String Swap Function
void myStrSwap(char* str1, char* str2)
{
for (int i = 0; str1[i] != '\0' || str2[i] != '\0'; i++)
{
char temp = str1[i];
str1[i] = str2[i];
str2[i] = temp;
}
}
//STEP 5
void myStrUpr(char* str)
{
for (int i = 0; str[i] != '\0'; i++)
{
str[i] = toupper(str[i]);
}
}
//STEP 6
void myStrLwr(char* str)
{
for (int i = 0; str[i] != '\0'; i++)
{
str[i] = tolower(str[i]);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started