C-String Functions without using <cstring>

Hello,

I've written 6 different c-string functions as shown. My problem is that program does not continue after the myStrCpy call and does nothing in the terminal after displaying "Input string has been stored in a safe place."

I don't get any code errors so I'm guessing something is wrong with my approach. Any advice would be greatly appreciated. Thanks!

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
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

//Function Prototypes
int myStrLen(const char*);
void myStrCpy(char*, const char*);
int myStrCmp(const char*, const char*);
void myStrSwap(char*, char*);
void myStrUpr(char*);
void myStrLwr(char*);

int main(){
char myArray[]={};//Initialize arrays
char copyArray[]={};

  cout << "Enter a string: " << endl;
  cin.getline(myArray, 100);//store user input
  int length = myStrLen(myArray);//get length of string
  cout << "The string you entered is: " << length << " characters long." << endl;//outputs return value of string length function

  myStrCpy(copyArray, myArray);//copy user's string into a second string
  if (myStrCmp(copyArray, myArray)==0)//compares two strings to see if they are the same
    cout << "String1 is the same as String2" << endl;
  else//if they aren't the same
    cout << "String1 is not the same as String2" << endl;
}

int myStrLen(const char* t){//Length function.
  int i=0;
  for (i; t[i]!='\0'; i++){}//checks to see if it has reached the end of the string.

return i; //return the length of string
}

void myStrCpy(char *s, const char *t){//copies array 't' and stores in array 's'
  int i=0;
  for(i=0;t[i]!='\0';i++){//checks to see if the end of the string has been reached
    s[i]=t[i];//stores ith element of t in the ith element of s
  }
  cout << "\nInput string has been stored in a safe place." << endl;
}

int myStrCmp(const char* s, const char* t){//compares the two arrays
  int i=0, flag=0;
  while (s[i]!='\0' && t[i]!='\0'){//checks to see if the end of the string has been reached
    if (s[i]!=t[i]){//if they aren't equal, breaks out of loop
      flag=1;
      break;
    }
  }
  if (flag==0 && s[i]=='\0' && t[i]=='\0')
    return 0;//once the end of the string has been reached
  else//strings are not equal if flag != 0
    return 1;
}

void myStrSwap(char* s, char* t){
  char *temp = t;//stores t in temp
  t=s;//stores s in t
  s=temp;//stores temp in s
}

void myStrUpr(char* t){//makes all element of string uppercase
  int i=0;
  for (i=0; i<100;i++){
    t[i]=toupper(t[i]);
    cout << t[i];
  }
}

void myStrLwr(char* t){//makes all elements of string lowercase
  for (int i=0; i<100; i++){
    t[i]=tolower(t[i]);
    cout << t[i];
  }
}
You never change the value of i in myStrCmp()
Put
i++;
after line 51.
Compiler errors:
line 15 [Error] zero-size array 'myArray'
line 16 [Error] zero-size array 'copyArray'

Logic error:
function myStrCpy() should add the null terminator after copying the characters.
Last edited on
Okay, I've removed those issues. However, now when I output the arrays, I get the question mark in a box character. If I remember correctly, this can happen because of the cout statement trying to read all 100 elements I allocated to the array in the beginning instead of ending at the null character. Not sure if that's what is happening here though. Thanks again.

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
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

//Function Prototypes
int myStrLen(const char*);
void myStrCpy(char*, const char*);
int myStrCmp(const char*, const char*);
void myStrSwap(char*, char*);
void myStrUpr(char*);
void myStrLwr(char*);


int main(){
char myArray[]={};//Initialize arrays
char copyArray[]={};

  cout << "Enter a string: " << endl;
  cin.getline(myArray, 100);//store user input
  int length = myStrLen(myArray);//get length of string
  cout << "The string you entered is: " << length << " characters long." << endl;//outputs return value of string length function

  myStrCpy(copyArray, myArray);//copy user's string into a second string

  if (myStrCmp(copyArray, myArray)==0)//compares two strings to see if they are the same
    cout << "String1 is the same as String2" << endl;
  else//if they aren't the same
    cout << "String1 is not the same as String2" << endl;

  myStrSwap(copyArray, myArray);
  myStrUpr(myArray);
  myStrLwr(myArray);
}

int myStrLen(const char* t){//Length function.
  int i=0;
  for (i; t[i]!='\0'; i++){}//checks to see if it has reached the end of the string.

return i; //return the length of string
}

void myStrCpy(char *s, const char *t){//copies array 't' and stores in array 's'
  int i=0;
  for(i=0;t[i]!='\0';i++){//checks to see if the end of the string has been reached
    s[i]=t[i];//stores ith element of t in the ith element of s
  }
  s[i]='\0';//adds null character to the end of the copy string
  for (i=0;s[i]!='\0';i++)
    cout << s[i] << endl;
}

int myStrCmp(const char* s, const char* t){//compares the two arrays
  int i=0, flag=0;
  while (s[i]!='\0' && t[i]!='\0'){//checks to see if the end of the string has been reached
    if (s[i]!=t[i]){//if they aren't equal, breaks out of loop
      flag=1;
      break;
    }
    i++;//increase counter
  }
  if (flag==0 && s[i]=='\0' && t[i]=='\0')
    return 0;//once the end of the string has been reached
  else//strings are not equal if flag != 0
    return 1;
}

void myStrSwap(char* s, char* t){
  char *temp = t;//stores t in temp
  t=s;//stores s in t
  s=temp;//stores temp in s
  for (int i=0; s[i]!='\0' && t[i]!='\0';i++){
  cout << t[i] << endl;
  cout << s[i] << endl;
  }
}

void myStrUpr(char* t){//makes all element of string uppercase
  int i=0;
  for (i=0; t[i]!='\0';i++){
    t[i]=toupper(t[i]);
    cout << t[i];
  }
}

void myStrLwr(char* t){//makes all elements of string lowercase
  for (int i=0; t[i]!='\0'; i++){
    t[i]=tolower(t[i]);
    cout << t[i];
  }
}
Okay, I've removed those issues.

No you have not.

Lines 16-17: How big do you think those arrays are? Hint: As Chervil pointed out, those lines cause compile errors.
16
17
char myArray[]={};//Initialize arrays
char copyArray[]={};

These arrays are still size zero. It doesn't compile for me.
I'm not sure what your compiler is doing, but it surely will not be guessing that you wanted a size of 100. The consequence of that is that there will be a buffer overflow, the program will be overwriting areas of memory which are outside the arrays. This will give undefined behaviour - anything could happen.

Try adding these lines (for information purposes) just after that (at line 18)
18
19
    cout << "sizeof(myArray) " << sizeof(myArray) << '\n';
    cout << "sizeof(copyArray) " << sizeof(copyArray) << '\n';

and it will tell you how big the arrays are.


You can use that information at line 20. Instead of this:
 
    cin.getline(myArray, 100);
replace it with
 
    cin.getline(myArray, sizeof(myArray));
This way, you can be certain that the getline will not try to store anything beyond the boundaries of the array.

And ... of course, go back to lines 16 and 17 and specify an actual size for the arrays:
1
2
    char myArray[100] = {}; // Initialize arrays
    char copyArray[100] = {};



By the way, the function myStrSwap() won't work as it is. Trying to swap the pointers here will only affect what happens inside the function. The original arrays will be unchanged.


Thank you very much for your help! It seems I was confused about how character arrays are declared and how to store information in them.
Topic archived. No new replies allowed.