Question: Hi I need help with my C++ code. I need to put the code from the void functions in the functions that has pointers and
Hi I need help with my C++ code. I need to put the code from the void functions in the functions that has pointers and nothing in main. Also I need help in fixing the step 3 with only just one loop using return 0 and return 1 using if and else thanks.
#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*);
void testStep1();
void testStep2();
void testStep3();
void testStep4();
void testStep5();
void testStep6();
int main()
{
//STEP 1
testStep1();
//STEP 2
testStep2();
//STEP 3
testStep3();
//STEP 4
testStep4();
//STEP 5
testStep5();
//STEP 6
testStep6();
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]);
}
}
void testStep1(){
char step1[] = { "Star" };
cout << step1 << endl;
cout << "Length = " << myStrLen(step1) << endl << endl;
}
void testStep2(){
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;
}
void testStep3(){
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;
}
void testStep4(){
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;
}
void testStep5(){
char step5[] = { "toupper" };
cout << step5 << endl;
myStrUpr(step5);
cout << step5 << endl << endl;
}
void testStep6(){
char step6[] = { "TOLOWER" };
cout << step6 << endl;
myStrLwr(step6);
cout << step6 << endl << endl;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
