Hello everybody!
I'm quite new to C++ and as exercise I had to write a simple program, trying to duplicate the strcpy(), strlen() and strcmp() functions in C++. The code seems to be ok from syntax point of view but the results are quite unsatisfying :o. If anyone could point me out the mistake I would be very thankful! Thanks.
#include <iostream>
using namespace std;
unsigned max_buf = 255;
int str_len(char* const str)
{
int counter = 0;
while(((*str)++ != '\0') || (*str)++!='\r')
++counter;
return counter;
}
void str_cpy(char* new_s, const char* source)
{
while(*new_s++=*source++);
}
int str_cmp(char* const str1, char* const str2)
{
if((*str1)++!=(*str2)++) return 1;
return 0;
}
int main(void)
{
char str1[max_buf];
char str2[max_buf];
char *a, *b;
cout << "\n Input string 1: ";
cin >> str1;
a = str1;
cout << "\n Input string 2: ";
cin >> str2;
b = str2;
cout << "\n The length of string 1 is: " << str_len(a);
cout << "\n The length of string 2 is: " << str_len(b);
if(!str_cmp(a, b))
cout << "\n The strings are identical!";
else
cout << "\n The strings are different!";
char* tmp1 = new char[str_len(a) + 1];
char* tmp2 = new char[str_len(b) + 1];
str_cpy(tmp1, a);
str_cpy(tmp2, b);
cout << "\n The concatated strings look like this: "
<< tmp1 << tmp2;
delete[] tmp1;
delete[] tmp2;
return 0;
}
<< moderator edit: added [code][/code] tags >>