hello, we recently reached pointers in my class and that section just blew me away badly. my professor gave us some practice lab to hep us get familiar, I'm having a hard time, please if someone can just explain this lab and help me fill in the codes with explanations.
This lab, based on K and R (aka: THE C PROGRAMMING LANGUAGE by Kernighan and Ritchie),
is designed to give practice in working with common C library functions used in C++/C:
• char* strcpy_(char* q, const char* p);
• char* strncpy_(char* q, const char* p, size_t n); // use care w/logic re: unsigned
• size_t strlen_(const char* p);
• char* strcat_(char* q, const char* p);
• int strcmp_(const char* p, const char* q);
All of these functions should be implemented with pointers. Students will also show each of the algorithms using spreadsheets (see strlen example below). Suggestion: use shorter character arrays to make the spreadsheets smaller.
Excellent websites that provide coding examples and documentations for these functions can be seen at:
http://www.cplusplus.com and
http://en.cppreference.com.
#include <iostream>
//
// Copies the C string pointed by source into the array pointed by destination,
// including the terminating character (and stopping at that point).
// To avoid overflows, the size of the array pointed by destination shall be long enough to contain // the same C string as source (including the terminating null character), and should not overlap // in memory w/ source.
char* strcpy_(char* q, const char* p) {
// fill in code here
}
// Copies the first num characters of source to destination. If the end of the source C string (which is signaled // by a null-character) is found before num characters have been copied, destination is padded with zeros
char* strncpy_(char* q, const char* p, size_t n) {
// fill in code here
}
// Returns the length of the C string str.
// The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters
// between the beginning of the string and the terminating null character (without including the terminating null character itself). //
size_t strlen_(const char* p) {
// fill in code here
}
// Appends a copy of the source string to the destination string. The terminating null character in destination // is overwritten by the first character of source, and a null-character is included at the end of the new string formed // by the concatenation of both in destination. destination and source shall not overlap.
char* strcat_(char* s, const char* ct) {
// fill in code here
}
// Compares the C string str1 to the C string str2.
// This function starts comparing the first character of each string. If they are equal to each other,
// it continues with the following pairs until the characters differ or until a terminating null-character is reached. //
int strcmp_(const char* p, const char* q) {
// fill in code here
}
void dstAndSrc(const char* msg, const char* q, const char* p) {
std::cout << msg << "... dst is: '" << q << "', 'src is: '" << p << "'\n";
}
#define BUFFER_SIZE 100
int compare(const char* p, const char* q) {
int cmp;
// fill in code here
return cmp; }
int main(int argc, const char * argv[]) {
// utility function – optional
std::cout << "C++/C string functions to implement...\n\n"
<< "\tstrcpy_(char* dst, const char* src);\n"
<< "\tstrncpy(char* dst, const char* src, size_t n);"
<< "\tstrlen_(const char* str);\n"
<< "\tstrcat(char* dst, const char* src);\n"
<< "\tstrcmp(const char* lhs, const char* rhs);\n";
char a[BUFFER_SIZE], b[BUFFER_SIZE];
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
char fname[] = "Aaron";
char lname[] = " Rodgerstein";
// fill in code here
return 0; }
//------------------------------------------------------------------------------------------------------
OUTPUT
C++/C string functions to implement...
strcpy_(char* dst, const char* src);
strncpy(char* dst, const char* src, size_t n);
strlen_(const char* str);
strcat(char* dst, const char* src);
strcmp(const char* lhs, const char* rhs);
before strcpy_... dst is: '', 'src is: 'Aaron'
after strcpy_... dst is: 'Aaron', 'src is: 'Aaron'
before strncpy_ (8 chars incl. ' ')... dst is: '', 'src is: ' Rodgerstein'
after strncpy_ (8 chars incl. ' ')... dst is: ' Rodgers', 'src is: ' Rodgerstein'
'Aaron' concatenated with ' Rodgers' = 'Aaron Rodgers'
.................................
Aaron Rodgers
01234567890123456
strlen is: 13
philosopher is greater than than philosophy
momently is greater than than momentarily