not copying my array in output?

Feb 20, 2013 at 6:22am
so I have to write a program using non c functions to prompt users for input then to read as a char array to get amount of characters in each array then write a void function to put the two together and then output what the two arrays are when put together thought i did it right it compiles but then just outputs the same info?? please help need to complete by tomorrow!

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

#include <iostream>
  using std::cin;
  using std::cout;
  using std::endl;

#include <cstring>


int myStrLen(const char[]); // return length of null-terminated
// string stored in char array

void myStrCpy(char[], const char[]); // copy the contents of the 
// 2nd string into the 1st

int myStrCmp(const char[], const char[]); // return 0 if two strings
// are equal, else return 1

int main()
{
  cout << endl;
  
  char text[128];
  cout << "enter text: ";
  cin.getline(text, 128);
  char text2[128];
  cout << "enter text: ";
  cin.getline(text2, 128);
  
  
  cout << "String: " << text << endl;
  cout << "Length: " << myStrLen(text) << endl;
  cout << "String: " << text2 << endl;
  cout << "Length: " << myStrLen(text2) << endl;
  

  myStrCpy(text, text2);
  cout << "String: " << text << endl;
  cout << "string: " << text2 << endl;
 

  return 0;
}

int myStrLen (const char * input)
{
    //variable that counts the number of elements in your array
    int count = 0;

    //While loop that tests whether the end of the array has been reached
    while (*(input + count) != '\0')
    {
        //advance to the next element of the array
        ++count;
    }
    //return the size of the array
    return count;
}

void myStrCpy(char * text, const char * const text2)
{
	int index = 0;	

	while (*(text + index) = *(text2 + index) )
	{
		index++;
	}
}
Feb 20, 2013 at 6:53am
If I have correctly understood you are not combining the two arrays into one array. You are simply coping the second array in the first array and outputting the both that results in outputting the same content.

You need to write a function that will combine two arrays in one array.
Feb 20, 2013 at 7:12am
so how would that differ in relationship to my void function now?
Feb 20, 2013 at 7:19am
You should define an array that will have size equal to the sum of sizes of the original arrays. In your case it will be equal to 255 or you can use 256. And then you need to write one additional function that will append one character array to the end of other character array.
Last edited on Feb 20, 2013 at 7:19am
Feb 20, 2013 at 7:22am
this is how my main function has to look like after of course prompting for user input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  char text[128];
  cin.getline(text, 128);

  char abc[] = "ABC";

  cout << "String: " << text << endl;
  cout << "Length: " << myStrLen(text) << endl;
  if (myStrCmp(text, abc) == 0)
    cout << "Your string equals ABC" << endl << endl;
  else
    cout << "Your string does not equal ABC" << endl << endl;

  myStrCpy(text, "ABC");
  cout << "String: " << text << endl;
  cout << "Length: " << myStrLen(text) << endl;
  if (myStrCpy(text, abc) == 0)
    cout "The changed string equals ABC" << endl << endl;
  else
    cout << "The changed string does not equal ABC" << endl << endl;
 
}
Feb 20, 2013 at 7:26am
and i would be writing this array sum[256] = (text[128] + text2[128]) in my void function right?
Feb 20, 2013 at 7:51am
As I said if I understood you correctly you are right.
You need to copy at first text into sum with using myStrCpy and then you need to write a function that will append the second string to sum.
You can name the function as myStrCat.
Feb 20, 2013 at 8:09am
im lost i dont know how to write the function in void or the second to add to sum
Feb 20, 2013 at 8:18am
Try the following

1
2
3
4
5
6
void myStrCat(char *s1, const char * const s2)
{
	while ( *s1 ) ++s1;

	myStrCpy( s1, s2 );
}


The sequence of calls will look as

1
2
myStrCpy( sum, text );
myStrCat( sum, abc );
Feb 20, 2013 at 8:25am
ok but where am i declaring sum?
Feb 20, 2013 at 8:29am
and what would the declaration be would i have to write 2 void functions?
Feb 20, 2013 at 8:29am
In main() before using these calls.
Feb 20, 2013 at 8:31am
this is what i have adding your void
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
// Lab: 5 C Strings
// Programmer: Joshua Henry
// editor used: Notepad
// compiler used: Microsoft Visual studio 2010

#include <iostream>
  using std::cin;
  using std::cout;
  using std::endl;

#include <cstring>


int myStrLen(const char[]); // return length of null-terminated
// string stored in char array

void myStrCpy(char[], const char[]); // copy the contents of the 
// 2nd string into the 1st

int myStrCmp(const char[], const char[]); // return 0 if two strings
// are equal, else return 1

int main()
{
  cout << endl;
  cout << "Lab: 5 C Strings \n";
  cout << "Programmer: Joshua Henry \n";
  cout << endl;
  
  char abc[] = "ABC";

  cout << "String: " << text << endl;
  cout << "Length: " << myStrLen(text) << endl;
  if (myStrCmp(text, abc) == 0)
    cout << "Your string equals ABC" << endl << endl;
  else
    cout << "Your string does not equal ABC" << endl << endl;

  myStrCpy(text, "ABC");
  cout << "String: " << text << endl;
  cout << "Length: " << myStrLen(text) << endl;
  if (myStrCpy(text, abc) == 0)
    cout "The changed string equals ABC" << endl << endl;
  else
    cout << "The changed string does not equal ABC" << endl << endl;
}

int myStrLen (const char * input)
{
    //variable that counts the number of elements in your array
    int count = 0;
   	
    //While loop that tests whether the end of the array has been reached
    while (*(input + count) != '\0')
    {
        //advance to the next element of the array
        ++count;
    }
    //return the size of the array
    return count;
}

void myStrCat(char *s1, const char * const s2)
{
	while ( *s1 ) ++s1;

	myStrCpy( s1, s2 );
}
Feb 20, 2013 at 8:46am
You forgot to define myStrCpy
Feb 20, 2013 at 8:56am
ok apparently I am only taking 1 input? this is what the code looks like now getting a void illegal now and a few other errors
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>
  using std::cin;
  using std::cout;
  using std::endl;

#include <cstring>


int myStrLen(const char[]); // return length of null-terminated
// string stored in char array

void myStrCpy(char[], const char[]); // copy the contents of the 
// 2nd string into the 1st

int myStrCmp(const char[], const char[]); // return 0 if two strings
// are equal, else return 1

int main()
{
  cout << endl;

  
  char text[128];
  cout << "Please enter a word or phrase followed by the enter key: \n";
  cin.getline(text, 128);
   char abc[] = "ABC";

  cout << "String: " << text << endl;
  cout << "Length: " << myStrLen(text) << endl;
  if (myStrCmp(text, abc) == 0)
    cout << "Your string equals ABC" << endl << endl;
  else
    cout << "Your string does not equal ABC" << endl << endl;

 
  myStrCpy(text, abc);
  cout << "String: " << text << endl;
  cout << "Length: " << myStrLen(text) << endl;
 
 if (myStrCpy(text, abc) == 0)
    cout "The changed string equals ABC" << endl << endl;
  else
    cout << "The changed string does not equal ABC" << endl << endl;
}

int myStrlen(const char* input)
{
  int i = 0;
  while (input[i])
    i++;
  return i;
}

void myStrCpy(char text[], const char abc[])
{
	int i;
	int temp;
	for(i = 0; abc[i] != '\0';i++)
	{
		temp = abc[i];
		text[i]=temp;
	}
}
int myStrCmp(const char text[], const char abc[])
{
	int i;
	int res;
	int temptext,tempabc;
	for(i = 0; abc[i] != '\0';i++)
	{
		temptext=text[i];
		tempabc=abc[i];
		res=tempabc-temptext;
		if (res!=0)
		{
			return 1;
		}
	}
	return 0;
}
Feb 20, 2013 at 9:17am
so i have this but its giving me just the void function error 2120
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
#include <iostream>
  using std::cin;
  using std::cout;
  using std::endl;

#include <cstring>

int myStrLen(const char[]); // return length of null-terminated
// string stored in char array

void myStrCpy(char[], const char[]); // copy the contents of the 
// 2nd string into the 1st

int myStrCmp(const char[], const char[]); // return 0 if two strings
// are equal, else return 1

int main()
{
  cout << endl;

  char text[128];
  cout << "Please enter a word or phrase followed by the enter key: \n";
  cin.getline(text, 128);
   char abc[] = "ABC";

  cout << "String: " << text << endl;
  cout << "Length: " << myStrLen(text) << endl
  
  if (myStrCmp(text, abc) == 0)
    cout << "Your string equals ABC" << endl << endl;
  else
    cout << "Your string does not equal ABC" << endl << endl;
  
 
  myStrCpy(text, "ABC");
  cout << "String: " << text << endl;
  cout << "Length: " << myStrLen(text) << endl;
 
 if (myStrCpy(text, abc) == 0)
   cout << "The changed string equals ABC" << endl << endl;
 else
   cout << "The changed string does not equal ABC" << endl << endl;
  
}

int myStrlen(const char* input)
{
  int i = 0;
  while (input[i])
  {
    i++;
  }
    return i;
}

void myStrCpy(char text[], const char abc[])
{
    // no need for temp variable.
    int i ;
    for(i = 0; abc[i] != '\0'; i++)
      text[i] = abc[i];   
      text[i] = '\0' ;  // c-strings end with a nul character.
}
int myStrCmp(const char text[], const char abc[])
{
	int i;
	int res;
	int temptext,tempabc;
	
	for(i = 0; abc[i] != '\0'; i++)
	{
      temptext = text[i];
	  tempabc = abc[i];
	  res = tempabc - temptext;
	  if(res!=0)
	  {
		return 1;
      }
	}
	return 0;
}
Topic archived. No new replies allowed.