Question

In: Computer Science

This C++ assignment asks to write a function that determines if a C-string begins with a...

This C++ assignment asks to write a function that determines if a C-string begins with a specified prefix. It should have the following signature:

 bool starts(char *str, char *prefix) 

It should return true if str begins with prefix, false if not. It should return false if prefix is longer than str. See the table below for some examples of what your function should return for various cases:

str prefix returns
airplanes air true
airplanes abc false
airplanes plane false
airplanes airplane true
air airplane false


Assume that the pointers passed to it are pointers to valid, null-terminated C-Strings. If you wish, you can use the strlen() and strcmp() functions, but it isn't required.

Hint: it can make things easier to check if length of prefix > length of str. If so, you can immediately return false! If not, you can continue on with your check.

Solutions

Expert Solution

#include <iostream>
#include <cstring>
using namespace std;

bool starts(char *str, char *prefix)
{
int len1=strlen(str),len2=strlen(prefix);
char temp[len2+1];
if(len2>len1)
return false;
int i=0;
while(i<len2)
{
temp[i]=*(str+i);
i++;
}
temp[i]='\0';
if(strcmp(temp,prefix)==0)
return true;
return false;
}

int main()
{   
char str1[100],str2[100];
cin>>str1>>str2;
cout<<std::boolalpha<<starts(str1,str2)<<endl;
return 0;
}



Related Solutions

Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
In python write a function whose input is a string. This function determines the data type...
In python write a function whose input is a string. This function determines the data type of the input string. The data types can be a float, int, or string. Most pass the following assertions: assert determine_data_type('1.2') == float assert determine_data_type('4') == int assert determine_data_type('EAS503') == str
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
C++ Write a function that determines if there are three of the same value in a...
C++ Write a function that determines if there are three of the same value in a row in an array. The function returns a bool and is passed an array of ints and it's size as an int. Write a function that determines if there are three of the same line in a row in a file. The function returns a bool and is passed a string which contains the filename. Write a function that determines if there are two...
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
C++, Write a function, noPunct, that would take a string, go through the string character by...
C++, Write a function, noPunct, that would take a string, go through the string character by character, and push any character that is NOT a punctuation mark onto a stack but count the punctuation dropped. After going through the entire string, print the contents of the stack. Then print the number of punctuation dropped. Hint: use ispunct() library function (returns true if character is punctuation)
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation...
C# Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A permutation is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words. Input: Tact Coa Output: True (permutations: "taco cat", "atco cta", etc.) Comment your code to explain it.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT