I'm working on a program for one of my classes that goes through all of the str functions, it prints out the results of the function and then I must write a function that will do the same thing without using the librarys function. I got most of it working, but the mystrcat part is shakey, my second word that should be added to the first word appears twice, I looked at my loops and everything looks fine to me but it obviously isnt if it repeats the second word.
Here is an example of my output ....
Appending Part
Adding word 2 to word 1 with the librarys function: Hello Mr. Henry
Adding word 2 to word 1 with my function: Hello Mr. HenryMr. Henry
-----------------------------------------------
Mr. Henry is repeated for some reason.
Here is my code in main the librarys function of strcat is used, and then right underneath that my version called mystrcat is called, so the error must be in the function mystrcat.
Any ideas???
Code:
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
#include <cstring>
int mystrlen (char []);
void mystrcpy (char one [], char two []);
void mystrncpy (char one [], char two [], int count);
void mystrcat(char *one, char *two);
int main ()
{
int liblen;
int mylen;
char word1 [50] = "Hello ";
char word2 [10] = "Mr. Henry";
char string1 [50] =" ";
char string2 [50] ="COPY ME";
int num=6;
char string3 [50]=" ";
char string4 [50]="December";
char string5 [50]="a";
char string6 [50]="b";
char string7 [50]="Its A or D";
char string8 [50]="Is It A or D";
//String Len
liblen = strlen("Hello");
mylen = mystrlen("Hello");
cout<<"Length Part"<<endl<<endl;
cout<<"The length of Hello with the librarys function: "<<liblen<<endl;
cout<<"The length of Hello with my function: "<<mylen<<endl;
if (!(mylen==liblen))
{
cout<<"Your function doesn't work like the librarys"<<endl;
}
cout<<"-----------------------------------------------"<<endl;
cout<<""<<endl<<endl;
//String Cat
cout<<"Appending Part"<<endl<<endl;
cout<<"Adding word 2 to word 1 with the librarys function: "<<strcat(word1, word2)<<endl;
mystrcat(word1, word2);
cout<<"-----------------------------------------------"<<endl;
cout<<""<<endl<<endl;
//String Cpy
cout<<"Copying Part"<<endl<<endl;
cout<<"String 1: "<<string1<<endl;
cout<<"String 2: "<<string2<<endl;
cout<<"Copying string 2 to string 1 with the librarys function: "<<strcpy(string1, string2)<<endl;
mystrcpy(string1, string2);
cout<<"-----------------------------------------------"<<endl;
cout<<""<<endl<<endl;
//String Ncpy
cout<<"Copying Part With Integer"<<endl<<endl;
cout<<"String 1: "<<string3<<endl;
cout<<"String 2: "<<string4<<endl;
cout<<"Number of chars to be copyed: "<<num<<endl;
cout<<"Copying string 2 to string 1 with the librarys function: "<<strncpy(string3, string4,num)<<endl;
mystrncpy(string3, string4, num);
cout<<"-----------------------------------------------"<<endl;
cout<<""<<endl<<endl;
//String Cmp
cout<<"Compare Part"<<endl<<endl;
cout<<"String 1: "<<string5<<endl;
cout<<"String 2: "<<string6<<endl;
cout<<"Comparing string 1 with string 2 with the librarys function: "<<strcmp(string5, string6)<<endl;
cout<<"-----------------------------------------------"<<endl;
cout<<""<<endl<<endl;
/*String Search For First Occurrence
cout<<"Search Part"<<endl<<endl;
cout<<"String 1: "<<string7<<endl;
cout<<"String 2: "<<string8<<endl;
cout<<"Comparing string 1 with string 2 with the librarys function: "<<strstr(string7, string8)<<endl;
cout<<"-----------------------------------------------"<<endl;*/
cout<<""<<endl<<endl<<endl;
cout<<"Press enter to quit, scroll up for the begining problems";
cin.get ();
return 0;
}
int mystrlen (char input [])
{
int location=0;
while (!(input[location]=='\0'))
location++;
return location;
}
void mystrcpy (char one [], char two [])
{
int x=0;
while (!(x=='\0'))
{
one [x] = two [x];
}
cout<<"Copying string 2 to string 1 with my function: "<<one<<endl;
}
void mystrncpy (char one[], char two[], int count)
{
int x = 0;
while ((!(x=='\0'))&&(x<count))
{
one [x] = two [x];
}
cout<<"Copying string 2 to string 1 with my function: "<<one<<endl;
}
void mystrcat(char *one, char *two)
{
char final [100];
int lenone = strlen(one);
int lentwo = strlen(two);
int i;
i = lenone;
// writes one to final
while (i--)
{
final[i] = one[i];
}
i = lentwo;
// writes two to final
while (i--)
{
final[lenone+i] = two[i];
}
final[lenone+lentwo] = 0;
cout<<"Adding word 2 to word 1 with my function: " <<final<<endl;
}