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);
// (5 pts) Use subscripts.
// Concatenate (append) the second string onto the end of the first one
// (assume that the first has appropriate space)
// example:
// first string = "abc"
// second string = "def"
// resulting string = "abcdef"
void mjcStrCat(char [], const char []);
//initializes variables and calls the other functions
int main()
{
char cstring1[] = "This is the first string, ";
char cstring2[] = "and this is the second string";
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;
mjcStrCat(cstring1, cstring2);
std::cout << "In main, after mjcStrCat exits, C-String 1 is: " << cstring1 << 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;
}
//concatenates string1 and string2 into a new string, then string1 will point to it
void mjcStrCat(char test_string1[], const char test_string2[])
{
//gathers length of both strings, and creates a new string with the necessary space
int size_of_string1 = mjcStrLen(test_string1);
int size_of_string2 = mjcStrLen(test_string2);
int size_of_newstring = size_of_string1 + size_of_string2;
char new_string[size_of_newstring];
//copy both strings to new string
int index_of_newstring = 0;
for(index_of_newstring; index_of_newstring < size_of_string1; index_of_newstring++)
new_string[index_of_newstring] = test_string1[index_of_newstring];
for(int index_of_string2 = 0; index_of_string2 < size_of_string2; index_of_string2++)
{
new_string[index_of_newstring] = test_string2[index_of_string2];
index_of_newstring++;
}
//pointing first string to new string
std::cout << "New String is: " << new_string << std::endl;
std::cout << "Inside function, before assignment, C-String 1 is: " << test_string1 << std::endl;
test_string1 = new_string;
std::cout << "Inside function, after assignment, C-String 1 is: " << test_string1 << std::endl;
}
|