Manually Copying two C-Strings

Hey guys, got another conundrum for you. My wonderful college professor gave me an assignment which requires me to manually copy one c-string into another (amongst other things, but we'll get to those later if need be), and use only pointer arithmetic to boot!

I understand the concept of c-strings and everything (series of characters terminated by a null, can be declared using a char array or char pointer, etc) but I've been absolutely banging my head the past five hours trying to figure out how to copy each element over in my mjcStrCpy function below. Did some internet research, and a lot of them just say use strcpy from the cstring class, which of course doesn't help me at all.

The rest of the code should work just fine. The function prototypes are all his, as well as the comments. The rest of the code and comments are from yours truly. I can't change the prototypes he wants me to use, nor can I use <string.h>, <cstring>, or <string>. He's very, very particular about how he wants things done :P

As usual, thanks in advance for any tips or advice!

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
// Author: Matt
// File: mjccstring.cpp
// Operating System: Linux
// Compiler: g++
// Written: 2/2013
// Assigned: 2/7/2013
// Due: 2/15/2013
// Purpose: 	This program will take two strings as input from the user, and perform various C-String related tasks with them

#include <iostream>
#include <iomanip>

// mjccstring.cpp
//−−−−−−−−−−−−−−−−
// (3 pts) Use pointer arithmetic (no subscripts).
// Return the number of characters before the null
// Example: iiiStrLen("xyz") would return 3
int mjcStrLen(const char * const);

//−−−−−−−−−−−−−−−−

// (4 pts) Use pointer arithmetic (no subscripts).
// Copy the second string into the first one, overwriting the value
// in the first (assume that the first has appropriate space)
// example:
//   first string = "abcdef"
//   second string = "xyz"
//   resulting string = "xyz"
void mjcStrCpy(char * const, const char * const);

//initializes variables and calls the other functions
int main()
{
	char cstring1[] = "Blah, blah, blah, blah";
	char cstring2[] = "Yadda, yadda";
	std::cout << "C-String 1 is:  " << cstring1 << std::endl;
	std::cout << "C-String 2 is:  ";  //just to show that I know pointer arithmetic
	int index = 0;	
	while(*(cstring2 + index))
	{
		std::cout << (*(cstring2 + index));
		index++;
	}	
	std::cout << std::endl;
	std::cout <<  cstring1 << " contains " << mjcStrLen(cstring1) << " characters" << std::endl;
	std::cout <<  cstring2 << " contains " << mjcStrLen(cstring2) << " characters" << std::endl;
	mjcStrCpy(cstring1, cstring2);
	std::cout << "After copy, C-String 1 is:  " << cstring1 << std::endl;
	std::cout << "After copy, C-String 2 is:  " << cstring2 << std::endl;
	return 0;
}

//uses a while loop to count the number of characters in a C-string
int mjcStrLen(const char * const test_string)
{
	int length = 0;
	int index = 0;
	while(*(test_string + index)) //will be true until it hits the null character
	{
		length++;
		index++;
	}
	return length;
}

//copies string2 into string1, overwriting its contents using only pointer arithmetic
void mjcStrCpy(char * const test_string1, const char * const test_string2)
{
	int index = 0;
	bool end_of_string2 = false;
	do
	{
		//(test_string1 + index) = (*(test_string2 + index));
		if(!(*(test_string2 + index))) //will set flag to end loop after null character is copied over
			end_of_string2 = true;
		index++;
	}while((*(test_string1 + index)) || end_of_string2); //will also end loop if string1 is out of space
}
Last edited on
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
#include <iostream>

using std::endl;
using std::cout;

void mjcStrCpy(char * const test_string1, const char * const test_string2)
{
  int index = 0;
  bool end_of_string2 = false;
  
  while (*(test_string1 + index))
  {
    *(test_string1 + index) = 0;//set everything to 0
    if (*(test_string2 + index))
      *(test_string1 + index) = *(test_string2 + index);
    index++;
  }
}


int main()
{
  char cstring1[] = "Blah, blah, blah, blah";
  char cstring2[] = "Yadda, yadda";
  
  cout << "cstring1 contains : " << cstring1 << endl;
  
  mjcStrCpy(cstring1, cstring2);
  
  cout << "cstring1 contains : " << cstring1 << endl;
  return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//uses a while loop to count the number of characters in a C-string
int mjcStrLen( const char * const test_string )
{
	int length = 0;

	while( *( test_string + length ) ) //will be true until it hits the null character
	{
		length++;
	}

	return length;
}

//copies string2 into string1, overwriting its contents using only pointer arithmetic
void mjcStrCpy(char * const test_string1, const char * const test_string2)
{
	int index = 0;

	while ( *( test_string1 + index ) = *( test_string2 + index ) )
	{
		index++;
	}
}
Last edited on
1
2
3
4
5
6
// copies string srce into string srce, overwriting its contents using only pointer arithmetic
// invariant: srce is a null terminated c-style string, dest is big enough to hold a copy of srce

inline void mjcStrCpy( char* dest, const char* srce ) { while( ( *dest++ = *srce++ ) ) ; }

// TODO: does this work? explain: why it does not work? / how does it work? 
Thanks for the assist! Code works perfect now

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 <iomanip>

int mjcStrLen(const char * const);
void mjcStrCpy(char * const, const char * const);

//initializes variables and calls the other functions
int main()
{
	char cstring1[] = "Blah, blah, blah";
	char cstring2[] = "Yadda, yadda, yadda";
	std::cout << "C-String 1 is:  " << cstring1 << std::endl;
	std::cout << "C-String 2 is:  " << cstring2 << std::endl;
	std::cout <<  cstring1 << " contains " << mjcStrLen(cstring1) << " characters" << std::endl;
	std::cout <<  cstring2 << " contains " << mjcStrLen(cstring2) << " characters" << std::endl;
	mjcStrCpy(cstring1, cstring2);
	std::cout << "After copy, C-String 1 is:  " << cstring1 << std::endl;
	std::cout << "After copy, C-String 2 is:  " << cstring2 << std::endl;
	std::cout <<  cstring1 << " contains " << mjcStrLen(cstring1) << " characters" << std::endl;
	std::cout <<  cstring2 << " contains " << mjcStrLen(cstring2) << " characters" << std::endl;	
	return 0;
}

//uses a while loop to count the number of characters in a C-string
int mjcStrLen(const char * const test_string)
{
	int length = 0;
	int index = 0;
	while(*(test_string + index)) //will be true until it hits the null character
	{
		length++;
		index++;
	}
	return length;
}

//copies string2 into string1, overwriting its contents using only pointer arithmetic
void mjcStrCpy(char * const test_string1, const char * const test_string2)
{
	int index = 0;
	while (*(test_string1 + index)) //looping through string1's characters until it hits the null
	{
		*(test_string1 + index) = 0; //sets current index to 0
		if(*(test_string2 + index)) //if string2 has a non-null char
			*(test_string1 + index) = *(test_string2 + index); //assigns it to string1
		index++;
	}
}
Last edited on
Your function mjcStrCpy is invalid, I showed you how to write the function correctly. It seems that you do not read what others wrote to you,
Last edited on
Topic archived. No new replies allowed.