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 >>

Recommended Answers

All 2 Replies

First, there is a difference between char* const str and const char* str. In char* const str, the pointer may not be modified, but what it points to may be. But this means you can't increment the pointer. A const char* str means that the pointer can be changed (incremented), but what it points to cannot be changed.

Also (*str)++ means to increment the thing that str is pointing to, not increment the pointer.

Now, why would your str_len check for '\r'?

In your str_cmp don't you think you need a loop?

Hello again! I am sorry for the delay with the answer. This is not how the str_cmp() looks like on my PC ;). I have probably made a wrong paste, which is definately my mistake. I have some purely technical problems, which will be over around the end of this working week and I will provide the right code, as well as ask some new stuff that purplexes me.. I wish you all a nice week and thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.