Question

In: Computer Science

strlen(): strlen() counts the number of characters in a cstring. The algorithm works by looking at...

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

Solutions

Expert Solution

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


Related Solutions

In C++, cstring is implemented as an array of characters. What is the difference between cstring...
In C++, cstring is implemented as an array of characters. What is the difference between cstring and a regular array of characters? In other words, how do you distinguish them?
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Create a program in C that counts the number of characters in a word when a...
Create a program in C that counts the number of characters in a word when a user inputs a string. Use stdin to read an input string. For example, if a user inputs: “The dog is good” the output should be a= [The], b=3 a= [dog], b=3 a= [ is], b=2 a= [good], b=4 a= [ ], b=0000 Take into account EOF. If an EOF is reached at the end of the string then the output should be 0000. (example...
Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
I need a C++ program using while loops that counts the number of characters in a...
I need a C++ program using while loops that counts the number of characters in a sentence. The user inputs a sentence and then terminates the input with either '.' or '!'. And then it needs to count and display the number of a's, e's, i's, o's, u's, and consonants. The program should read both lower and upper case. They don't want us using switch statements or string operators, and want us to us if else if statements. I have...
Write a C program that counts the number of non white-space characters in an inputtext file....
Write a C program that counts the number of non white-space characters in an inputtext file. Program takes as command argument(s)the name of the input file (and the output file with option -f). Based on the option flags, it displays the output on the standard output, or writesthe output to an output file. The command format is as follows: command -f inputfile outputfile or, command -s inputfile -f indicates writing to an output file; -s indicates displaying the output on...
PYTHON: Describe a recursive algorithm that counts the number of nodes in a singly linked list.
PYTHON: Describe a recursive algorithm that counts the number of nodes in a singly linked list.
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions...
C++ Develop program in C++ using arrays of characters, subscript operator, the cstring library, and functions with arguments. Create programs with small functions where main goes to a series of functions where the real work takes place. Don’t use global variables and don’t use break within a loop (unless working with a switch statement). Functions can’t have more than 30 statements of code, not including comments, blank lines, or variable definitions. Don’t use a return in the middle of the...
Having trouble with the Luhn algorithm for java. Everything works great except for when cthe number...
Having trouble with the Luhn algorithm for java. Everything works great except for when cthe number 5105105105105109 is entered. I'm sure there are other numbers that do this, but this returns check digit as 10 when it should come back as 0. Suggestions? here is the algorithm and then my current code without the prints because my error is in the math. I am not allowed to use break. Another key part of the credit card number standard is the...
Design an algorithm that will read an array of 200 characters and display to the screen...
Design an algorithm that will read an array of 200 characters and display to the screen a count of the occurrences of each of the five vowels (a, e, i, o, u) in the array. Your string is: The quick brown fox jumped over the lazy dog That is the question for my pseudo code question, I don't know if I have this correct can i have someone look this over and finish this and explain what i needed to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT