Question: Need help about C++ Strings. I have some code written but the steps say that I need to use only one loop for eqach function
Need help about C++ Strings. I have some code written but the steps say that I need to use only one loop for eqach function and test for the array using, e.g., if (array[i] == 0), or if (array[i] == '\0'). I do not need to use the functions of the C++ strings library I need to write my own strings. 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[] = { "Hello" };
cout
cout
//STEP 2
char step2a[] = { "This" };
char step2b[] = { "That" };
cout
cout
myStrCpy(step2a, step2b);
cout
cout
//STEP 3
char step3a[] = { "Those" };
char step3b[] = { "These" };
cout
cout
if (myStrCmp(step3a, step3b) == 0)
cout
else
cout
//STEP 4
char step4a[] = { "Them" };
char step4b[] = { "They" };
cout
cout
myStrSwap(step4a, step4b);
cout
cout
//STEP 5
char step5[] = { "toupper" };
cout
myStrUpr(step5);
cout
//STEP 6
char step6[] = { "TOLOWER" };
cout
myStrLwr(step6);
cout
system("pause");
}
//STEP 1
int myStrLen(const char* str) {
int len = 0;
for (int i = 0; str[i] != '\0'; i++) {
len += 1;
}
return len;
}
//STEP 2
void myStrCpy(char* dest, const char* src) {
int srcLen = 0;
for (int i = 0; src[i] != '\0'; i++) {
srcLen += 1;
}
for (int i = 0; i
dest[i] = src[i];
}
dest[srcLen] = '\0';
}
//STEP 3
int myStrCmp(const char* str1, const char* str2) {
int str1Len = 0;
for (int i = 0; str1[i] != '\0'; i++) {
str1Len += 1;
}
int str2Len = 0;
for (int i = 0; str2[i] != '\0'; i++) {
str2Len += 1;
}
int same = 0;
if (str1Len == str2Len) {
for (int i = 0; i
if (str1[i] == str2[i])
same++;
}
if (same == str1Len || same == str2Len)
return 0;
else
return 1;
}
else
return 1;
}
//STEP 4
void myStrSwap(char* str1, char* str2) {
int str1Len = 0;
for (int i = 0; str1[i] != '\0'; i++) {
str1Len += 1;
}
int str2Len = 0;
for (int i = 0; str2[i] != '\0'; i++) {
str2Len += 1;
}
if (str1Len >= str2Len) {
for (int i = 0; i
char temp = str1[i];
str1[i] = str2[i];
str2[i] = temp;
}
str1[str2Len] = '\0';
str2[str1Len] = '\0';
}
if (str1Len
for (int i = 0; i
char temp = str2[i];
str2[i] = str1[i];
str1[i] = temp;
}
str1[str2Len] = '\0';
str2[str1Len] = '\0';
}
}
//STEP 5
void myStrUpr(char* str) {
int len = 0;
for (int i = 0; str[i] != '\0'; i++) {
len += 1;
}
for (int i = 0; i
str[i] = toupper(str[i]);
}
}
//STEP 6
void myStrLwr(char* str) {
int len = 0;
for (int i = 0; str[i] != '\0'; i++) {
len += 1;
}
for (int i = 0; i
str[i] = tolower(str[i]);
}
}
These are the steps:


![function and test for the array using, e.g., if (array[i] == 0),](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f545f12dbd1_25666f545f08dd5e.jpg)
![or if (array[i] == '\0'). I do not need to use the](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f545f215490_25766f545f17a47f.jpg)
STEP 1 of 7: Write A C-String Length Function C++ has one, but C has no "string" data type. So we use arrays of characters to achieve the same functionality. Write a C function to compute C string length, using this function prototype int myStrLen(const char);return length of null-terminated C string stored in char array Refer to your notes from our lectures on arrays, for how to write this function. Remember to search for the null character, '1o,to define the end of a C string i.e., the character in the array after the last character of the C string. Test for it using, e.g., if (array[i] 0), or if (array[i] ='\0'). Do not use pointers use array syntax instead. Do not use more than one loop in the function Be sure to fully test the function by putting several calls in a main) function, using a variety of different C strings. Name the CPP file as you wish - - it is not until step 7 that you will be asked to submit this function in a program named Strings.cpp. Do not use
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
