String Functions

Sep 8, 2019 at 10:56pm
I'm currently writing a program that uses string functions. I need some advice/hints on how I can display "Hello World" and its length with myStrcat() in main().

My current 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
#include <iostream>
#include <cstring>
#include <string>

using namespace std;

int myStrlen(char str1[])
{
    int i = 0;
    for (i=0; str1[i] != '\0'; i++)
    str1[i] = '\0';

    return i;
}

int myStrcat(char str2[], char str3[])
{

}

int myStrcpy(char str4[], char str5[])
{
    int i = 0;
    for (i=0; str5[i] != '\0'; i++)
    str4[i] = str5[i];
    str4[i] = '\0';

    return i;
}

int main()
{
    const int SIZE = 11;
    char s1[SIZE] = "Hello";
    char s2[SIZE] = "World";

    cout << "s1: " << " " << s1 << endl << endl; ///Should display "Hello"
    cout << "The length of s1: " << myStrlen(s1) << endl << endl;

    cout << "Doing strcat(s1, s2) " << endl;
    myStrcat(s1, s2);
    cout << "s1: " << " " << s1 << endl; /// Should display "Hello World"
    cout << "The length of s1: " << myStrlen(s1) << endl << endl;

    cout << "Doing strcpy(s1, s2) " << endl;
    myStrcpy(s1, s2);
    cout << "s1: " << " " << s1 << endl; /// Should display "World"
    cout << "The length of s1: " << myStrlen(s1) << endl << endl;


My current output:
1
2
3
4
5
6
7
8
9
10
11
s1:  Hello

The length of s1: 5

Doing strcat(s1, s2)
s1:
The length of s1: 0

Doing strcpy(s1, s2)
s1:  World
The length of s1: 5
Sep 9, 2019 at 12:18am
your strlen is destroying the string by replacing all the chars with 0 end of string.
why?

your strcat is empty. you need to code it?
Its pretty much strcpy called twice.
something like:
myStrcpy(a,b);
myStrcpy(&a[strlen(a)],c);
that is, copy b into a, then copy c into a starting at the end of where the b data is. Double check the position, I didn't look very closely.
Last edited on Sep 9, 2019 at 12:19am
Sep 9, 2019 at 2:03am
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
#include <iostream>
#include <iomanip> // for std::quoted

int my_strlen( const char cstr[] ) // note: const
{
    int len = 0 ;
    for( ; cstr[len] != 0 ; ++len ) ;
    // or: while( *cstr++ ) ++len ;
    return len ;
}

void my_strcpy( char target[], const char source[] ) // note: source of the copy is const
{
    // exit the loop once the null terminating character is copied
    for( int pos = 0 ; ( target[pos] = source[pos] ) != 0 ; ++pos ) ;
    // or: while( *target++ = *source++ ) ;
}

void my_strcat( char target[], const char source[] ) // note: source is const
{
    // use my_strcpy to copy to the end of the target string
    my_strcpy( target + my_strlen(target), source ) ;
}

void print( const char cstr[] ) // note: const
{
    std::cout << "string " << std::quoted(cstr)
              << " of length " << my_strlen(cstr) << '\n' ;
}

#define PRINT(s) ( ( std::cout << #s << ": " ), print(s) )

int main()
{
    const int N = 100 ;
    char a[N] = "hello" ;
    char b[N] = "" ;

    PRINT(a) ;
    PRINT(b) ;

    std::cout << "\nmy_strcpy(b,a)\n" ;
    my_strcpy( b, a ) ;
    PRINT(a) ;
    PRINT(b) ;

    const char space[] = " " ;
    std::cout << "\nmy_strcat( b, space )\n" ;
    PRINT(space) ;
    my_strcat( b, space ) ;
    PRINT(b) ;

    const char world[] = "world" ;
    std::cout << "\nmy_strcat( b, world )\n" ;
    PRINT(world) ;
    my_strcat( b, world ) ;
    PRINT(b) ;

    const char excl[] = "!" ;
    std::cout << "\nmy_strcat( b, excl )\n" ;
    PRINT(excl) ;
    my_strcat( b, excl ) ;
    PRINT(b) ;
}

http://coliru.stacked-crooked.com/a/b25fb556485da461
Topic archived. No new replies allowed.