Question

In: Computer Science

Look up the man pages for the following string functions from the C standard library, and...

Look up the man pages for the following string functions from the C standard library, and write implementations of each:

  • strstr()

Please explain the work please, thank you

Solutions

Expert Solution

In C++, std::strstr() is a predefined function used for string handling. string.h is the header file required for string functions.

This function takes two strings s1 and s2 as an argument and finds the first occurrence of the sub-string s2 in the string s1. The process of matching does not include the terminating null-characters(‘\0’), but the function stops there.

Implement in C:

#include <stdio.h>

// returns true if X and Y are same
int compare(const char *X, const char *Y)
{
   while (*X && *Y)
   {
       if (*X != *Y)
           return 0;

       X++;
       Y++;
   }

   return (*Y == '\0');
}

// Function to implement strstr() function
const char* strstr(const char* X, const char* Y)
{
   while (*X != '\0')
   {
       if ((*X == *Y) && compare(X, Y))
           return X;
       X++;
   }

   return NULL;
}

// Implement strstr function in C
int main()
{
   char *X = "Techie Delight - Coding made easy";
   char *Y = "Coding";

   printf("%s\n", strstr(X, Y));

   return 0;
}

Return Value: This function returns a pointer points to the first character of the found s2 in s1 otherwise a null pointer if s2 is not present in s1. If s2 points to an empty string, s1 is returned.


Related Solutions

In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: For this exercise you should be able to write a logical expression (i.e., with logical operators) which checks if some integer x consists of exactly 5 digits. Ex: 30498 and -14004 are 5-digit numbers, while 1098, -1 and 34 are not. Complete the intQ2(intQ2_input) function that takes an input integer...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: A positive integer number is said to be a perfect number if its positive factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Complete the int Q6(intQ6_input, int perfect[])function that determines all perfect numbers smaller than or equal...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: (Pythagorean Triples) A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: Complete the int Q7a(intQ7_input) function takes only a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: Complete the int Q7a(intQ7_input) function takes a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use arrays...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: intQ1_for() intQ1_while() intQ1_do() To compute the sum of all numbers that are multiples of 4, between 30 and 1000, in three different ways: with a for loop, a while loop and a do-while loop, accordingly. After each loop print the value on the screen. Return the total sum at the...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password...
-Write in C++ -Use Char library functions Write a function that accepts a string representing password and determines whether the string is a valid password. A valid password as the following properties: 1. At least 8 characters long 2. Has at least one upper case letter 3. Has at least one lower case letter 4. Has at least one digit 5. Has at least on special character
We will write a simple program that simply uses the three standard library functions. The following...
We will write a simple program that simply uses the three standard library functions. The following is a high level description of the main program (main.cpp). In this program, please do the following: Include the C-string library. #include <cstring> Declare the following C-style string: char line[] = "ls -l -a | wc -c >> myfile";. Declare a variable of type size_t named "len". Use the builtin strlen function to compute the length of the variable "line". Store the result in...
Please write code in c++ using iostream library. Also you can use any string library. Create...
Please write code in c++ using iostream library. Also you can use any string library. Create structure plane with the following: 1)plane's ID[int type] 2) location[char*] 3) departure time[char*] Your task is to find the plane with the smallest departure time for the selected city. Pointer, dynamic array and structures have to be used in this code. Input: NOTE: It's just an example of input, you should write code generally for any inputs. First line contains n(0 < n <...
PLEASE PROVIDE COMMENTS ON STEPS IMPORTANT NOTE: The library string functions cant be used. You must...
PLEASE PROVIDE COMMENTS ON STEPS IMPORTANT NOTE: The library string functions cant be used. You must use pointers and the switch statement to execute this program. Assume that the vowels are a, e, i, o, u. The modification has to be done in the original char array without creating a new array. Write a C++ program that modifies a string (null terminated) as follows: Consonants are positioned at the beginning of the string and vowels are moved to the end...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT