Question

C++ Coding Please. Thank you! 1) Complete a function, getStringInfo. Two given character strings are as...

C++ Coding Please. Thank you!

1) Complete a function, getStringInfo. Two given character strings are as below.

char *str1 = “my name is Amy!”;
char *str2 = “I got lost on my way to work”;

The function accepts two constant character strings, str1 and str2 as pointer. Exchange the sentence of the variables str1 and str2 and then compute and display both strings’ length using pointer. Return the sum of the lengths of str1 and str2

int getStringInfo(const char *str1, const char *str2)

{

}

Homework Answers

Answer #1
#include <iostream>

using namespace std;
int getStringInfo(const char *str1, const char *str2);//prototype
int main()
{
int sum_length; 
char *str1 = "my name is Amy!";
char *str2 = "I got lost on my way to work";
sum_length=getStringInfo(str1,str2);//calling the function and passing value and saving the output in sum_length
cout << "\nTotal Length = " << sum_length;//printing the total sum of length of both strings
    return 0;
}
int getStringInfo(const char *str1, const char *str2)
{

swap(str1,str2);//swaping strings
int Size1 = 0, Size2=0;//to count the size
    while (str1[Size1] != '\0') Size1++;//looping till end to find the length
    while (str2[Size2] != '\0') Size2++;
    cout<<"\nLength of String 1 ="<<Size1;
    cout<<"\nLength of String 2 ="<<Size2;
    return Size1+Size2;//returning the sum of length


}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
C++ only: Please implement a function that accepts a string parameter as well as two character...
C++ only: Please implement a function that accepts a string parameter as well as two character arguments and a boolean parameter. Your function should return the number of times it finds the character arguments inside the string parameter. When both of the passed character arguments are found in the string parameter, set the boolean argument to true. Otherwise, set that boolean argument to false. Return -1 if the string argument is the empty string. The declaration for this function will...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
**please write code with function definition taking in input and use given variable names** for e.g....
**please write code with function definition taking in input and use given variable names** for e.g. List matchNames(List inputNames, List secRecords) Java or Python Please Note:    * The function is expected to return a STRING_ARRAY.      * The function accepts following parameters:      *  1. STRING_ARRAY inputNames      *  2. STRING_ARRAY secRecords      */ Problem Statement Introduction Imagine you are helping the Security Exchange Commission (SEC) respond to anonymous tips. One of the biggest problems the team faces is handling the transcription of the companies reported...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT