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