Question

Write a function to find the last occurrence of a string within another string. The function...

Write a function to find the last occurrence of a string within another string. The function should use the prototype below. Use pointer arithmetic when traversing the strings. (C Language)

char * search_string(const char * str, const char * substr);

Homework Answers

Answer #1

Source Code

#include <stdio.h> 
#include <string.h> 

// Function to reverse the string 
// using pointers 
void reverseString(char* str) 
{ 
        int l, i; 
        char *begin_ptr, *end_ptr, ch; 

        // Get the length of the string 
        l = strlen(str); 

        // Set the begin_ptr and end_ptr 
        // initially to start of string 
        begin_ptr = str; 
        end_ptr = str; 

        // Move the end_ptr to the last character 
        for (i = 0; i < l - 1; i++) 
                end_ptr++; 

        // Swap the char from start and end 
        // index using begin_ptr and end_ptr 
        for (i = 0; i < l / 2; i++) { 

                // swap character 
                ch = *end_ptr; 
                *end_ptr = *begin_ptr; 
                *begin_ptr = ch; 

                // update pointers positions 
                begin_ptr++; 
                end_ptr--; 
        } 
} 

// Driver code 
int main() 
{ 

        // Get the string 
        char str[100] = "GeeksForGeeks"; 
        printf("Enter a string: %s\n", str); 

        // Reverse the string 
        reverseString(str); 

        // Print the result 
        printf("Reverse of the string: %s\n", str); 

        return 0; 
} 

The Output

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
Write a function to check whether string s1 is a substring of string s2. The function...
Write a function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. For example, the function should return 2 if s1 is "to" and s2 is "October". If s1 is "andy" and s2 is "candy", then the function should return 1. The function prototype is as follows: int indexOf(const char *s1, const char *s2) Write a program to test the function.
3. Write function, leastChar(inputString) that takes as input a string of one or more letters (and...
3. Write function, leastChar(inputString) that takes as input a string of one or more letters (and no other characters) and prints 1) the "least" character in the string, where one character is less than another if it occurs earlier in the alphabet (thus 'a' is less than 'C' and both are less than 'y') and 2) the index of the first occurrence of that character. When comparing letters in this problem, ignore case - i.e. 'e' and 'E' should be...
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...
You may notice that the function strchr in the <cstring> library searches for a character within...
You may notice that the function strchr in the <cstring> library searches for a character within a string, and returns the memory address (pointer) to where that character first occurs. 1.) Write your own version of the strchr function, int findchr( char text[ ], char target) , which returns the index of the first occurrance of the character target if it occurs within the string text. If target does not occur within the string, a -1 is to be returned....
FOR C PROGRAMMING LANGUAGE Write a recursive C PROGRAMMING LANGUAGE function int sumStrlens(const char* str1, const...
FOR C PROGRAMMING LANGUAGE Write a recursive C PROGRAMMING LANGUAGE function int sumStrlens(const char* str1, const char* str2) which returns the sum of strlen(str1) and strlen(str2). You can assume that strlen(str2) is always strictly greater than strlen(str1) (strlen(str2) > strlen(str1)). You MAY NOT use any function from string.h. You MAY NOT use any square brackets([]) or any type of loops in function.
USE C++ Write a function named find that takes a pointer to the beginning and a...
USE C++ Write a function named find that takes a pointer to the beginning and a pointer to the end (1 element past the last) of an array, as well as a value. The function should search for the given value and return a pointer to the first element with that value, or the end pointer if no element was found.
Complete a function definition in C for strmatch. For this problem, you can't use any <string.h>...
Complete a function definition in C for strmatch. For this problem, you can't use any <string.h> library functions. In c, two strings match exactly if they have the same characters in the same order and same length. Consider this: int strmatch(const char *x, const char *b); //Needs each of x and y points to the beginning of a string. //Promises to return value 1 if the two strings match exactly. If they don't match, the return value is 0. For...
/* Write a function that looks for a particular person in their respective linked list The...
/* Write a function that looks for a particular person in their respective linked list The only place you need to write code is the "find" method in the linked list class */ #include <bits/stdc++.h> using namespace std; string ltrim(const string &); string rtrim(const string &); #define BUFFLEN 10 /* Each "person" is defined by their name, zipcode, and their pet's name. Persons are hashed by their zipcode. */ //---------------------------------------------------------------------------- /* function declarations ------------------------*/ int computeKey(int); void add_to_buffer(string,int,string); void find_in_buffer(int);...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
Write code in C++, using the correct headers not the catchall std/bitsc: Write a recursive, bool-valued...
Write code in C++, using the correct headers not the catchall std/bitsc: Write a recursive, bool-valued function, containsVowel, that accepts a string and returns true if the string contains a vowel. A string contains a vowel if: The first character of the string is a vowel, or The rest of the string (beyond the first character) contains a vowel it should be making use of true/false as it is a boolean, substr(), find, and !=.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT