Please help with my code

I have to create c style functions header file and then create a cpp file to use the functions this is my header file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef CSTRING_H_INCLUDED
#define CSTRING_H_INCLUDED

/**
*Parameters:
* s1
* C string.
* Return Value:
* The length of s1.
**/
int Strlen(const char s1[ ]);

/**
* Parameters:
* s1
* Destination array where
* the content is to be copied.
* s2
* C string to be copied.
* Return Value:
* void.
**/
void Strcpy(char s1[ ], const char s2[ ]);

/**
Parameters:
* s1
* C string to be compared.
* s2
* C string to be compared.
* Return Value:
* Returns an integral value
* indicating the relationship between the strings:
* A zero value indicates that both strings are equal.
* A value greater than zero indicates that the
* first character that does not match has a greater
* value in s1 than in s2; And a value less than zero
* indicates the opposite.
**/
int Strcmp(const char s1[ ], const char s2[ ]);

/**
* Parameters:
* s1
* C string
* s2
* C string to be added to the end of s1.
* Return Value:
* void
**/
void Strcat(char s1[ ], const char s2[ ]);

#endif // CSTRING_H_INCLUDED


this is my main code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include "Cstring.h"

using namespace std;

int Strlen(const char s1[])
{
    int len = 0;
    for(int i=0;s1[i] != '\0';i++)
    len++;
    return len;
}

void Strcpy(char s1[], const char s2[])
{
    int len = 0;
    for(int i=0;s2[i] != '\0';i++)
    len++;
    for(int i=0;i<len;i++)
    {
        s1[i] = s2[i];
    }
}

int Strcmp(const char s1[], const char s2[])
{
    int flag = 1; //Assuming strings are equal
    int len = 0;

    for(int i=0;s2[i] != '\0';i++)
    len++;
    for(int i=0;i<len;i++)
    {
        if(s1[i] != s2[i])
            flag = 0; //Strings not equal

    }

    if(flag == 1)
    return 0;
    else if(s1[0] > s2[0])
    return 1;
    else
    return -1;

}

void Strcat(char s1[], const char s2[])
{
    int len1 = 0, j = 0;
    for(int i=0;s1[i] != '\0';i++)
    len1++;

    for(int i=len1; s2[i] != '\0' ; i++)
    {
        s1[i] = s2[j];
        j++;
    }
}
int main()
{
    //declaring s1 and s2
    char s1[20] = "";
    char s2[20] = "!!!Hello World!!!";
    char s3[20] = "";



    //checking Strlen()
    cout<<"Strlen(s1) = "<<Strlen(s1)<<" - "<<s1<<endl;
    cout<<"Strlen(s2) = "<<Strlen(s2)<<" - "<<s2<<endl;
    cout<<"Strlen(s3) = "<<Strlen(s3)<<" - "<<s3<<endl;


    //checking Strcpy()
    Strcpy(s2,s1);
    Strcpy(s2,s3);
    cout<<"\nStrlen(s1) = "<<Strlen(s1)<<" - "<<s1<<endl;
    cout<<"Strlen(s2) = "<<Strlen(s2)<<" - "<<s2<<endl;
    cout<<"Strlen(s3) = "<<Strlen(s3)<<" - "<<s3<<endl;


this what my output is supposed to look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Strlen(s1) = 0 - 
Strlen(s2) = 17 - !!!Hello World!!!
Strlen(s3) = 39 - What do you want to set aside time for?

Strlen(s1) = 17 - !!!Hello World!!!
Strlen(s2) = 17 - !!!Hello World!!!
Strlen(s3) = 17 - !!!Hello World!!!

Same
Same
Not Same

Strlen(s1) = 51 - !!!Hello World!!!!!!Hello World!!!!!!Hello World!!!
Strlen(s2) = 20 - !!!Hello World!!!***
Strlen(s3) = 34 - !!!Hello World!!!!!!Hello World!!!


I have no ideawhat im doing wrong please help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
int my_strlen(const char s[])
{ int n = 0; while (s[n]) ++n; return n; }

int my_strcmp(char const* a, char const* b)
{ while (*a && *a == *b) ++a, ++b; return *a - *b; }

void my_strcpy(char dst[], const char src[])
{ do *dst++ = *src; while (*src++); }

void my_strcat(char dst[], const char src[])
{ dst += my_strlen(dst); my_strcpy(dst, src); }

#include <cstdio>
#include <cstring>

int main()
{
  { 
    char s1[] = "";
    char s2[] = "!!!Hello World!!!";
    char s3[] = "What do you want to set aside time for?";
    
    char const* fmt = "my_strlen(\"%s\") = %2d\n";
    std::printf(fmt, s1, my_strlen(s1));
    std::printf(fmt, s2, my_strlen(s2));
    std::printf(fmt, s3, my_strlen(s3));
  }
  
  std::puts("");
  
  { 
    char s1[] = "";
    char s2[] = "!!!Hello World!!!";
    char s3[] = "What do you want to set aside time for?";
    char buf[sizeof s3 + sizeof s2 + sizeof s1];
    
    char const* fmt = "my_strcpy(buf, \"%s\"); buf = \"%s\"\n";
    my_strcpy(buf, s1); std::printf(fmt, s1, buf);
    my_strcpy(buf, s2); std::printf(fmt, s2, buf);
    my_strcpy(buf, s3); std::printf(fmt, s3, buf);
    my_strcpy(buf, s2); std::printf(fmt, s2, buf);
  }
  
  std::puts("");
  
  { 
    char s1[] = "";
    char s2[] = "!!!Hello World!!!";
    char s3[] = "What do you want to set aside time for?";
    char buf[sizeof s3 + sizeof s2 + sizeof s1 + sizeof s2];
    
    char const* fmt = "my_strcat(buf, \"%s\"); buf = \"%s\"\n";
    my_strcat(buf, s1); std::printf(fmt, s1, buf);
    my_strcat(buf, s2); std::printf(fmt, s2, buf);
    my_strcat(buf, s3); std::printf(fmt, s3, buf);
    my_strcat(buf, s2); std::printf(fmt, s2, buf);
    my_strcat(buf, s1); std::printf(fmt, s1, buf);
  }  
  
  std::puts("");
  
  { 
    char s1[] = "";
    char s2[] = "!!!Hello World!!!";
    char s3[] = "What do you want to set aside time for?";
    
    char const* fmt = "my_strcmp(\"%s\", \"%s\") = %2d\n";
    std::printf(fmt, s1, s1, my_strcmp(s1, s1));
    std::printf(fmt, s1, s2, my_strcmp(s1, s2));
    std::printf(fmt, s1, s3, my_strcmp(s1, s3));
    std::printf(fmt, s2, s1, my_strcmp(s2, s1));
    std::printf(fmt, s2, s3, my_strcmp(s2, s3));
    std::printf(fmt, s3, s1, my_strcmp(s3, s1));
    std::printf(fmt, s2, s2, my_strcmp(s2, s2));
  }
}
Well, first things first, you are missing at least one line of code after line 80 in main, the closing }. The code as provided won't compile.

Next up:

Lines 63 & 65 create two blank C strings (s1 & s3), so the first output of s3 should be the same as the first output of s1.

The two string copies at lines 76-77 copy blank C strings into your one C string that contained actual data more than being blank. Now all 3 C strings are blank.

FYI, there is an output tag that works better than using code tags for the output. See 2. in the article:
http://www.cplusplus.com/articles/z13hAqkS/
Perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
size_t Strlen(const char* s) {
    const char* sc {s};

    for (; *sc; ++sc);
    return sc - s;
}

void Strcpy(char* s1, const char* s2) {
    while (*s1++ = *s2++);
}

int Strcmp(const char* s1, const char* s2) {
    for (; *s1 == *s2; ++s1, ++s2)
        if (!*s1)
            return 0;

    return *reinterpret_cast<const unsigned char*>(s1) < *reinterpret_cast<const unsigned char*>(s2) ? -1 : 1;
}

void Strcat(char* s1, const char* s2) {
    for (; *s1; ++s1);
    while (*s1++ = *s2++);
}

Topic archived. No new replies allowed.