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)
{
}
#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
}
Get Answers For Free
Most questions answered within 1 hours.