so I have to write a program using non c functions to prompt users for input then to read as a char array to get amount of characters in each array then write a void function to put the two together and then output what the two arrays are when put together thought i did it right it compiles but then just outputs the same info?? please help need to complete by tomorrow!
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <cstring>
int myStrLen(constchar[]); // return length of null-terminated
// string stored in char array
void myStrCpy(char[], constchar[]); // copy the contents of the
// 2nd string into the 1st
int myStrCmp(constchar[], constchar[]); // return 0 if two strings
// are equal, else return 1
int main()
{
cout << endl;
char text[128];
cout << "enter text: ";
cin.getline(text, 128);
char text2[128];
cout << "enter text: ";
cin.getline(text2, 128);
cout << "String: " << text << endl;
cout << "Length: " << myStrLen(text) << endl;
cout << "String: " << text2 << endl;
cout << "Length: " << myStrLen(text2) << endl;
myStrCpy(text, text2);
cout << "String: " << text << endl;
cout << "string: " << text2 << endl;
return 0;
}
int myStrLen (constchar * input)
{
//variable that counts the number of elements in your array
int count = 0;
//While loop that tests whether the end of the array has been reached
while (*(input + count) != '\0')
{
//advance to the next element of the array
++count;
}
//return the size of the array
return count;
}
void myStrCpy(char * text, constchar * const text2)
{
int index = 0;
while (*(text + index) = *(text2 + index) )
{
index++;
}
}
If I have correctly understood you are not combining the two arrays into one array. You are simply coping the second array in the first array and outputting the both that results in outputting the same content.
You need to write a function that will combine two arrays in one array.
You should define an array that will have size equal to the sum of sizes of the original arrays. In your case it will be equal to 255 or you can use 256. And then you need to write one additional function that will append one character array to the end of other character array.
As I said if I understood you correctly you are right.
You need to copy at first text into sum with using myStrCpy and then you need to write a function that will append the second string to sum.
You can name the function as myStrCat.
// Lab: 5 C Strings
// Programmer: Joshua Henry
// editor used: Notepad
// compiler used: Microsoft Visual studio 2010
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <cstring>
int myStrLen(constchar[]); // return length of null-terminated
// string stored in char array
void myStrCpy(char[], constchar[]); // copy the contents of the
// 2nd string into the 1st
int myStrCmp(constchar[], constchar[]); // return 0 if two strings
// are equal, else return 1
int main()
{
cout << endl;
cout << "Lab: 5 C Strings \n";
cout << "Programmer: Joshua Henry \n";
cout << endl;
char abc[] = "ABC";
cout << "String: " << text << endl;
cout << "Length: " << myStrLen(text) << endl;
if (myStrCmp(text, abc) == 0)
cout << "Your string equals ABC" << endl << endl;
else
cout << "Your string does not equal ABC" << endl << endl;
myStrCpy(text, "ABC");
cout << "String: " << text << endl;
cout << "Length: " << myStrLen(text) << endl;
if (myStrCpy(text, abc) == 0)
cout "The changed string equals ABC" << endl << endl;
else
cout << "The changed string does not equal ABC" << endl << endl;
}
int myStrLen (constchar * input)
{
//variable that counts the number of elements in your array
int count = 0;
//While loop that tests whether the end of the array has been reached
while (*(input + count) != '\0')
{
//advance to the next element of the array
++count;
}
//return the size of the array
return count;
}
void myStrCat(char *s1, constchar * const s2)
{
while ( *s1 ) ++s1;
myStrCpy( s1, s2 );
}