In: Computer Science
strlen():
strlen() counts the number of characters in a cstring. The algorithm works by looking at each character in the cstring, counting as it goes. It stops looking at characters when it encounters a null terminator ‘\0’.
Here is my implementation of the strlen() function. You will need to write and test my solution and your own solution.
int profStrLen( char *str ) {
char *endOfStr = str;
while( *endOfStr != '\0' ) {
endOfStr++;
}
return (int)(endOfStr - str);
}
Make sure you understand how my implementation is working before creating your own. Then, you can write your own implementation. For your implementation, do not use pointer arithmetic to solve it - use array notation and a counter instead. Here is the prototype for your function.
int stuStrLen( char * );
strcpy():
strcpy() copies the contents of the second char* into the memory of the first char*. The algorithm works by looking at each character in the second char* and copies that into the first char*. Because we are dealing with cstrings, the copying process will stop when the ‘\0’ is reached in the second char*.
Here’s my implementation. Again, you will need to write and test my solution and your own.
char *profStrCpy( char *str1, char *str2 ) {
char *dest = str1;
while( *str2 != '\0' ) {
*str1 = *str2;
str1++;
str2++;
}
*str1 = ‘\0’;
return dest;
}
Make sure you understand how my implementation is working before creating your own. Then, you can write your own implementation. For your implementation, do not use pointer arithmetic to solve it - use array notation and a counter instead. Here is the prototype for your function.
char *stuStrCpy( char *, char * );
strcat():
strcat() copies the contents of the second char* into the memory of the first char* like strcpy(), but it copies the values in the second char* to the end of the first char*. The algorithm works by looking at each character in the first char* until the ‘\0’ is found. Then, it looks at each character in the second char* and copies that into the first char*’s current location. As it does this, it increments the position in each char* until the ‘\0’ is found in the second char*.
Here’s my implementation. Again, you will need to write and test my solution and your own.
char *profStrCat( char *str1, char *str2 ) {
char *dest = str1;
while( *str1 != '\0' ) {
str1++;
}
while( *str2 != '\0' ) {
*str1 = *str2;
str1++;
str2++;
}
*str1 = ‘\0’;
return dest;
}
Make sure you understand how my implementation is working before creating your own. Then, you can write your own implementation. For your implementation, do not use pointer arithmetic to solve it - use array notation and a counter instead. Here is the prototype for your function.
char *stuStrCat( char *, char * );
Intermediate cstring function
Now that you have worked through many of the cstring functions. I want you to implement the following functions on your own. I will provide an overview of the algorithm, but I will not provide you with an implementation from me. You will need to write and test this function.
strcmp():
strcmp() compares strings lexicographically. It returns a negative value if the first string occurs lexicographically before the second string. It returns a positive value if the first string occurs lexicographically after the second string. And, it returns zero if the two strings are equivalent. Make sure you call attention to the return of strcmp(). The return of zero instead of a non-zero is a common source for errors involved with cstring processing.
The algorithm for strcmp() works by subtracting each character as it iterates through each cstring stopping when the result of the subtraction is nonzero or a ‘\0’ is found in one of the strings. We will return the difference between the two characters.
You will need to write and test your solution. Here is your prototype.
int stuStrCmp( char *, char * );
Everybody likes a mystery
Here is a function of code that is labeled mystery. Type in the code and describe in the comments what the function does algorithmically and tell me what you think the function is named.
int mystery( char *str ) {
char *str2 = str;
long pos;
int result = 0;
while( *str2 != '\0' ) {
str2++;
}
pos = str2 - str - 1;
for( int count = 1; pos >= 0; pos --, count *= 10 ) {
if( str[pos] == ‘-') {
result *= -1;
} else {
result += (str[pos] - '0') * count;
}
}
return result;
}
main.cpp
#include <iostream>
using namespace std;
int profStrLen( char * );
int stuStrLen( char * );
char *profStrCpy( char *, char * );
char *stuStrCpy( char *, char * );
char *profStrCat( char *, char * );
char *stuStrCat( char *, char * );
int stuStrCmp( char *, char * );
int mystery( char * );
int main() {
// type all testing code here
return 0;
}
// implement the functions here
Here is the solution to above problem. All the functions have been modified with array notation
The mystery function converts string to integer for example if string is "760" it will return the value 760 as integer.
C++ CODE
#include<iostream>
#include<string>
using namespace std;
//string len
int stuStrLen( char *str ) {
//crate a var
int i=0;
//till str[i] not becomes null increment the i
while( str[i] != '\0' ) {
i++;
}
//return i as string len
return i;
}
char * stuStrCpy( char *str1, char *str2 ) {
int i=0; //create var
//traverse till source gets null character
while( str2[i] != '\0' ) {
str1[i]= str2[i]; //copy into destination
i++; ///increment len
}
str1[i]= '\0'; // add null character
return str1;
}
//no need to return call by reference
char * stuStrCat( char *str1, char *str2 ) {
int i=0; //for traversal
while( str1[i] != '\0' ) {
i++;
}
int j=0; //to traverse string 2
while( str2[j] != '\0' ) {
str1[i++] = str2[j++]; //copy str2[j] into str1[i] incrment both i
and j
}
str1[i] = '\0';
return str1;
}
//compare two strings
int stuStrCmp(char *str1,char *str2)
{
int len1=0; //for lenth of first string
while( str1[len1] != '\0' ) {
len1++;
}
int len2=0; //for lenth of second string
while( str1[len2] != '\0' ) {
len2++;
}
int i=0; // for traversal
while(i<len1&&i<len2)
{
if(str1[i]-str2[i]!=0)
return str1[i]-str2[i];
i++;
}
//if one of the string was short
if(i<len1)
return str1[i];
if(i<len2)
return str2[i];
}
//this mystery function converts string to Integer
int mystery( char *str ) {
char *str2 = str;
long pos;
int result = 0;
while( *str2 != '\0' ) {
str2++;
}
pos = str2 - str - 1;
for( int count = 1; pos >= 0; pos --, count *= 10 ) {
if( str[pos] == '-') {
result *= -1;
} else {
result += (str[pos] - '0') * count;
}
}
return result;
}
int main() {
// type all testing code here
char *str1= new char[10];
str1[0]='7';
str1[1]='6';
str1[2]='0';
str1[3]='\0';
char *str2= new char[10];
str2[0]='5';
str2[1]='8';
str2[2]='7';
str2[3]='\0';
char *str3= new char[10];
cout<<"Str 1: "<<str1<<endl;
cout<<"Str 2: "<<str2<<endl;
cout<<"String length str1:
"<<stuStrLen(str1)<<endl;
cout<<"String length str2:
"<<stuStrLen(str2)<<endl;
str3= stuStrCpy(str3,str2);
cout<<"Copied String str3 is:
"<<str3<<endl;
str1 = stuStrCat(str1,str2);
cout<<"Concatenate Str 1 and Str 2:
"<<str1<<endl;
cout<<"Compare String str1 and str2:
"<<stuStrCmp(str1,str2)<<endl;
cout<<"Str1 to Integer is :
"<<mystery(str1)<<endl;
return 0;
}
Screenshot of output
