Question

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 instance: the value of strmatch("xyz", "xyz") should be 1;

the value of strmatch("xy", "xyz") should be 0;

Homework Answers

Answer #1
#include <stdio.h>

int strmatch(const char *x, const char *b){
    int i = 0;
    while(!(x[i]=='\0' || b[i]=='\0')){
        if(x[i]!=b[i]){
            return 0;
        }
        i++;
    }
    if(x[i]=='\0' && b[i]=='\0'){
        return 1;
    }
    return 0;
}


int main()
{
    printf("%d\n",strmatch("xyz", "xyz"));
    printf("%d\n",strmatch("xy", "xyz"));
   return 0;
}

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
C++ Coding Please. Thank you! 1) Complete a function, getStringInfo. Two given character strings are as...
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,...
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...
C Programming:- What is the type of the variable x? uint8_t *x[256]; (Do not use spaces...
C Programming:- What is the type of the variable x? uint8_t *x[256]; (Do not use spaces in your answer) What type does the variable x decay to when used in an expression? uint8_t *x[256] (Do not use spaces in your answer) Consider the following function whose purpose is to return an array of 3 strings that are initialized to the strings in the character arrays s1, s2, and s3: #include <string.h> #include <stdlib.h> char** stringstoarray(char* s1, char* s2, char* s3)...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
Please answer the following C question: There is a documented prototype for a function called get_a_line...
Please answer the following C question: There is a documented prototype for a function called get_a_line in the code below. Write a definition for get_a_line—the only function called from that definition should be fgetc. #include <stdio.h> #include <string.h> #define BUFFER_ARRAY_SIZE 10 int get_a_line(char *s, int size, FILE *stream); // Does what fgets does, using repeated calls to fgetc, but // provides a more useful return value than fgets does. // // REQUIRES // size > 1. // s points to...
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e., by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, ’A’ is replaced by ’C’, ’B’ is...
In Python: This will require you to write several functions, and then use them in a...
In Python: This will require you to write several functions, and then use them in a program. Logical Calculator The logical calculator does math, but with logical operators. In logic, we represent a bit with 0 as false and a bit with 1 as true. The logical operators are NOT, AND and OR. Bitwise logical calculations operate on each bit of the input. The NOT operator works on just one three-bit argument. NOT 011 = 100 The AND operator works...
Implement and demonstrate a disk-based buffer pool class based on the LRU buffer pool replacement strategy....
Implement and demonstrate a disk-based buffer pool class based on the LRU buffer pool replacement strategy. Disk blocks are numbered consecutively from the beginning of the file with the first block numbered as 0. Assume that blocks are 4096 bytes in size. Use the supplied C++ files to implement your LRU Buffer Pool based on the instructions below. • Implement a BufferBlock class using the supplied BufferBlockADT.h o Your Buffer Block must inherit BufferBlockADT or you will not get credit...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...