#include<string>
int str_len( const char * const src)
{
    int len=0;
    while(src[len])
    { 
        len++;
    }
    return len;
}
int str_copy( char *  &dest,  char *  src)    // removed (const) to sore the string in dest.By using const,we dont have the 
                                             // access to store the string.By using ampersand we can return the copied string as well as
                                             // the length.
{
    int len=0;
    while(src[len])
    {
        dest=src;
        len++;

    }
    return len;
}
int str_cat( char *  &dest,  char * src1,  char *  src2)   // not working
{
    int len=0;
    while(dest=src1=src2)
    {
        dest++;
        src1++;
        src2++;
        len++;

    }
    return len;
}
int str_to_upper_case( char *  &dest, char *  src)
{
    int len=0;
    while(src[len])
    {
        dest=src-32;            //converting in to uppercase

    len++;
    }
    return len;
}
int main()
{
    char*s1="Testing the function ";
    char*s2="Testing str_cat";
    char*s3="abcd";
    char*s4;
    int length_s1=str_len(s1);
    int str_copy_len=str_copy(s1,s2);
    //int concatenate=str_cat(s3,s1,s2);
    int upp_case=str_to_upper_case(s4,s3);
    return 0;
}

please point out the mistakes...my concatenate function is not working as well as well as upper case function return the len accrately but didnt show the upper case character...it shows a square box while debugging.

C-string don't automatically grow. You have to be aware of the array's dimensions. Why don't you use 'string' objects instead as we're talking about C++ here?

That said, some comments on the code:

char*s1="Testing the function ";
char*s2="Testing str_cat";

While declared char*, note that they point to const char*. This means you won't be able to modify them. Declare them as follows instead:

char s1[] = "Testing the function ";
char s2[] = "Testing str_cat";

Then your copy function:

int str_copy( char *  &dest,  char *  src)
{
    int len=0;
    while(src[len])
    {
        dest=src;
        len++;
    }
    return len;
}

There is no need for a reference here, your method of copying is faulty too. You're assigning the pointers itself all the time. Change it to something like this:

// You should give the sizes of the arrays too.
// What will you do now when 'dest' is smaller than 'src'?
// I didn't fix that for you in the version below.
int str_copy(char* dest,  char* src)
{
    int len = 0;

    while(src[len])
    {
        dest[len] = src[len];
        len++;
    }

    // The loop stops at '\0' so have to append that seperately.
    dest[len] = '\0';

    return len;
}

Note I only fixed what was wrong in your code. The function itself is still very error-proof because you don't check the bounds of the arrays.

Then your concatenate function:

int len=0;
    while(dest=src1=src2)
    {
        dest++;
        src1++;
        src2++;
        len++;
    }
    return len;

Well, this code just doesn't make sense. Your condition in the while statement will pretty much always be true, and in the loops body you don't assign anything you just increment the pointers and the length.

Fixing the function would result in something like this. Note that again I do no bounds checking, I expect you to add that:

int str_cat(char* dest,  char* src1,  char*  src2)
{
    // First copy the first string.
    int characters_copied = str_copy(dest, src1);

    // Then copy the second.
    return characters_copied + str_copy(dest + characters_copied, src2);
}

Your upper case function contains similar problems. One of the things you do wrong is that you assume character should always be decremented with 32. This is not true and you should check on it:

int str_to_upper_case(char* dest, char* src)
{
    int len=0;

    while(src[len])
    {
        // Only convert the character if it's a alpha character in lower case.
        if (src[len] >= 'a' && src[len] <= 'z')
        {
            dest[len] = src[len] - 32;
        }
        else
        {
            dest[len] = src[len];
        }

        len++;
    }

    // And again, don't forget the NULL character.
    dest[len] = '\0';

    return len;
}

Here's your whole code fixed. I included the iostream library to print the results of some tests. But again, the string class has most of the things you want to do, and if it hasn't there are other libraries that do. I assume this was for practicing purposes.

#include<string>
#include<iostream>

using namespace std;


int str_len( const char * const src)
{
    int len=0;
    while(src[len])
    {
        len++;
    }
    return len;
}

// You should give the sizes of the arrays too.
// What will you do now when 'dest' is smaller than 'src'?
// I didn't fix that for you in the version below.
int str_copy(char* dest,  char* src)
{
    int len = 0;

    while(src[len])
    {
        dest[len] = src[len];
        len++;
    }

    // The loop stops at '\0' so have to append that seperately.
    dest[len] = '\0';

    return len;
}

int str_cat(char* dest,  char* src1,  char*  src2)
{
    // First copy the first string.
    int characters_copied = str_copy(dest, src1);

    // Then copy the second.
    return characters_copied + str_copy(dest + characters_copied, src2);
}

int str_to_upper_case(char* dest, char* src)
{
    int len=0;

    while(src[len])
    {
        // Only convert the character if it's a alpha character in lower case.
        if (src[len] >= 'a' && src[len] <= 'z')
        {
            dest[len] = src[len] - 32;
        }
        else
        {
            dest[len] = src[len];
        }

        len++;
    }

    // And again, don't forget the NULL character.
    dest[len] = '\0';

    return len;
}

int main()
{
    char s1[] = "Testing the function ";
    char s2[] = "Testing str_cat";
    char s3[100]; // Destination for strcat. Size of the array should be big enough to hold both s1 and s2.
    char s4[100]; // Should be equally big as s3 in this case.

    cout << "String 1: \"" << s1 << "\"\n";
    cout << "String 2: \"" << s2 << "\"\n\n";

    cout << "The length of string 1: " << str_len(s1) << endl << endl;

    cout << "Copying string 2 to string 1.\n";
    cout << str_copy(s1,s2) << " character copied. String 1 is now: \"" << s1 << "\".\n\n";

    cout << "Concatenating string 1 and 2 and placing the result in string 3.\n";
    cout << "The new string is of size " << str_cat(s3,s1,s2) << " . It's contents: \"" << s3 << "\".\n\n";

    cout << "Converting string 3 into uppercase and placing the result into string 4.\n";
    cout << "Processed " << str_to_upper_case(s4,s3) << " characters. String 4: \"" << s4 << "\".\n";

    return 0;
}
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.