C-String & Class String From the Tony Gaddis text, the chapter on C-String and C
ID: 3685588 • Letter: C
Question
C-String & Class String
From the Tony Gaddis text, the chapter on C-String and Class String:
#1 String Length: Write a Function that passes in a C-String and using a pointer determine the number of chars in the string.
Data: “This is a test string for string length”
#2 Prt String Backwards: Write a Function that passes in a C-String and prints the string backwards. Data: “This is a test string for string length”
#3 replaceSubstring: Write a Function that accepts three C-Strings – str1, str2, str3. It should search in str1 all occurrences of str2 and replace str2 with str3.
Data 1: str1 = “the dog jumped over the fence”
str2 = “the”
str3 + “that”
Output: “that dog jumped over that fence”
Data 2: str1 = “Sid likes to eat nuts but the brother, Sidney likes to eat fruit. One time Sid put a pecan in Sidney’s banana and boy hidey did Sid get a chuckle out of Sidney’s misfortune”.
Str2 = “Sid”
Str3 = “Mikey”
#4 replaceSubstring: Same function as above (overloaded), but use Class Strings instead of C-Strings as arguments.
#5 Password Verifier: Some software requires a password but the password must meet certain standards. You are to write a code that verifies that a password has met the following conditions. If passwd is invalid, indicate which condition is not meet.
i. the passwd should be at least six characters long
ii. the passwd contains at least one upper and one lower case letter
iii. the passwd has at least one digit.
Data: ABC123 TwoFor1 Sid22 howdyYALL Seventy8 GorillaalliroG ThisIs4You
#6 Password Verifier: Same as above (overloaded), but using Class Strings.
#7 Word Separator: Write the code to accepts as input a sentence in which all of the words are run together, but the first character of each word is uppercase. Convert the sentence to a string in which the words are separated by spaces and only the first word starts with an uppercase letter.
For ex: str = “StopAndSmellTheRoses.” Would be converted to “Stop and smell the roses.”
Data: (don’t worry about the ‘S’s staying capital S’s for the names, lower case will be fine.)
“SidLikesToEatNutsButTheBrother,SidneyLikesToEatFruit.OneTimeSidPutAPecanInSidney’sBananaAndBoyHideyDidSidGetAChuckleOutOfSidney’sMisfortune”.
You may put all the above in one program file or have separate programs. Label the output so I know which answers go with which questions. Comment your code so I can see which routines solve which problems. Cut and pasted the above data sets into a file for reading in the data.
Explanation / Answer
1.To determine the number of characters in a string:
Length=string_length(str); this is function call where string is passed to the function
int string_length(char*z) /* z=&str[0] */ z is the pointer here
{
int counthere = 0;
while (*z != '') {
counthere++;
z++;
}
return counthere;
}
2.print the string backwards:
void reverse(str)
{int i = 0;
int j;
int temporary;
j = strlen(str) - 1;
while (i < j) {
temporary = str[i];
str[i] = str[j];
str[j] = temporary;
i++;
j--;
}
printf(" Reverse string is :%s", str);
return (0);
}
}
3. void replacesubstring(char string1[10].char string2[10],char string3[10)
{
void replacesubstring(char string1[10].char string2[10],char string3[10)
{ char *strPtr;
strPtr = strstr(string1, string2);
if (strPtr == NULL)
{
cout << " There is Nothing found" << endl;
}
if (strPtr)
{
cout << "the same word has been found" << endl;
strncpy (strPtr,string3,6);
puts (string1);
}
4.
void replacesubstring(string s1, string s2,string s3)
{ char *strPtr;
strPtr = strstr(s1, s2);
if (strPtr == NULL)
{
cout << " There is Nothing found" << endl
}
if (strPtr)
{
cout << "the same word has been found" << endl;
strncpy (strPtr,s3,6);
cout<<s1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.