C++ confusing question. Hi, I have my program already done but I do not what the
ID: 3596607 • Letter: C
Question
C++ confusing question. Hi, I have my program already done but I do not what the assignment is trying to say in step 7. Do I need to put everything in functions instead of main? And if so how can I put it. Thanks.
This is my code:
#include <iostream>
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] != ''; 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] != ''; i++)
{
dest[i] = src[i];
srcLen += 1;
}
dest[srcLen] = '';
}
//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] != '' && str2[i] != ''; i++)
{
if (str1[i] != str2[i])
{
return 1;
}
}
if (str1[i] == '' && str2[i] == '')
{
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] != '' || str2[i] != ''; i++)
{
char temp = str1[i];
str1[i] = str2[i];
str2[i] = temp;
}
}
//STEP 5
void myStrUpr(char* str)
{
for (int i = 0; str[i] != ''; i++)
{
str[i] = toupper(str[i]);
}
}
//STEP 6
void myStrLwr(char* str)
{
for (int i = 0; str[i] != ''; i++)
{
str[i] = tolower(str[i]);
}
}
STEP 7 of 7: Combine All Functions Into A Test Program Create a new console application named Include in it the functions you developed and tested in step 1-6 above: myStrLen, myStrCmp, myStrCpy, myStrSwap, myStrUpr, and myStrLwr. To do so, simply copy and paste them into Strings.cpp. Include enough testing to convince yourself that the functions will work right in any C/C++ program that uses them. You can assume that the user of your functions will properly size their Cstrings, and include null terminators to mark the ends of strings[Your program will not be scored using your main!Explanation / Answer
I got that meaning of that step7. You need to write all 6 functions which actually step(1-6). Then in step7, you need to merge all functions to "Strings.cpp". And last statement states that "Your program will not be scored using main!" means ,"if you not written all functions separately or if written all in single main function", then it is not correct.
Moreever Strings.cpp should not include main().
Step1-6 --> all functions in Strings.cpp
Write main() as separate .cpp file, since this Strings.cpp file must able to usable by any program.
So finally..
strings.h
#ifndef STRINGS_H // To make sure you don't declare the function more than once by including the header multiple times.
#define STRINGS_H
//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*);
#endif
--------------------------------------------------------------------------------------------------------------------------------------------------
strings.cpp
#include <iostream>
#include "strings.h"
using std::cout;
using std::cin;
using std::endl;
//STEP 1 of 7: C - String Length Function
int myStrLen(const char* str)
{
int len = 0;
for (int i = 0; str[i] != ''; 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] != ''; i++)
{
dest[i] = src[i];
srcLen += 1;
}
dest[srcLen] = '';
}
//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] != '' && str2[i] != ''; i++)
{
if (str1[i] != str2[i])
{
return 1;
}
}
if (str1[i] == '' && str2[i] == '')
{
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] != '' || str2[i] != ''; i++)
{
char temp = str1[i];
str1[i] = str2[i];
str2[i] = temp;
}
}
//STEP 5
void myStrUpr(char* str)
{
for (int i = 0; str[i] != ''; i++)
{
str[i] = toupper(str[i]);
}
}
//STEP 6
void myStrLwr(char* str)
{
for (int i = 0; str[i] != ''; i++)
{
str[i] = tolower(str[i]);
}
}
-------------------------------------------------------------------------------------------------------------------------------------------------
main.cpp
#include <iostream>
#include "strings.h"
using std::cout;
using std::cin;
using std::endl;
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");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.