Question

Write a program in C to find the number of occurrences a given word appears in...

Write a program in C to find the number of occurrences a given word appears in the given string:

int LeterInAString(char c, char *s);   Check whether the string is NULL. Also, the char could be any char including special symbols.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

int LeterInAString(char c, char *s);

int main() {
    char str[100];
    char ch;
    printf("Enter a string: ");
    scanf("%s", str);
    printf("Enter a letter: ");
    scanf(" %c", &ch);
    printf("Count of %c in %s is %d\n", ch, str, LeterInAString(ch, str));
    return 0;
}

int LeterInAString(char c, char *s) {
    if (s == NULL) return 0;
    int count = 0, i = 0;
    while (s[i]) {
        if (s[i] == c)
            ++count;
        i++;
    }
    return count;
}
Add a comment
Know the answer?
Add Answer to:
Write a program in C to find the number of occurrences a given word appears in...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified...

    Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified character in a string) Write a recursive function that finds the number of occurrences of a specified letter in a string using the following function header. int count(const string& s, char a) For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a string and a character, and displays the number of occurrences for the character in the...

  • The program needs to be written in C. Write a function void camelCase(char* word) where word...

    The program needs to be written in C. Write a function void camelCase(char* word) where word consists of more than two words separated by underscore such as “random_word” or "hello_world_my_name_is_sam". camelCase() should remove underscores from the sentence and rewrite in lower camel case” (https:// en.wikipedia.org/wiki/Camel_case). Watch out for the end of the string, which is denoted by ‘\0’. You have to ensure that legal strings are given to the camelCase() function. The program should only run when the input is...

  • Write a function that, given a word and a set of characters, filters all occurrences of...

    Write a function that, given a word and a set of characters, filters all occurrences of those characters from the word. For e.g. remove_chars("abcdeeeeefmnop", "ebo") returns: acdfmnp i.e all occurrences of e, b and o have been removed from abcdeeeeefmnop You should acquire the word and set of characters both from the user and then pass them to the function. Note: std::cin terminates when it encounters a space, you should take the word and character set as separate inputs: std::cin...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • Write a C program RemoveWord.c that can remove a word from a given string (i.e. a...

    Write a C program RemoveWord.c that can remove a word from a given string (i.e. a line) entered by the user Sample output is as following: Please enter a string: This is the last homework. Please enter the word to be removed: is Result: This the last homework. Hint: The C library function gets reads a line from stdin and stores it into the string pointed to by str. char *gets(char *str)

  • Take the following C++ code and add to it the following: Write a program that reads...

    Take the following C++ code and add to it the following: Write a program that reads a string and outputs the number of times each lowercase vowel appears in it. Your program must contain a function with one of its parameters as a char variable, and if the character is a vowel, it increments that vowel's count. #include<iostream> #include<string> using namespace std; int countVowel(char, char); int main(void) { string str1; int countA = 0; int countE = 0; int countI...

  • C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank...

    C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank you. Use the given function to create the program shown in the sample run. Your program should find the name of the file that is always given after the word filename (see the command line in the sample run), open the file and print to screen the contents. The type of info held in the file is noted by the word after the filename:...

  • Given a String str, and a char c return the number of times that c appears...

    Given a String str, and a char c return the number of times that c appears inside str. countChar("Abcdefg", 'a') returns 1 countChar("xxXx", 'x') returns 4 countChar("", 'q'') returns 0 ----------------- Please use java public class Count { public int countProblem(String str, char c) { int res = 0; //Your work is here return result; } }

  • C++ SECTION 1) (15 points) Write a full C++ program that reads an integer, a list...

    C++ SECTION 1) (15 points) Write a full C++ program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Ex: If the input is: 4 hello zoo sleep drizzle z then the output is: 200 drizzle To...

  • A. Write an Array Program Write a main program that counts the number of occurrences of...

    A. Write an Array Program Write a main program that counts the number of occurrences of the number 6.0 in a large array of doubles and then prints out the number of elements in the array and the number of values that are 6.0. Also, compute and print the average value to 7 decimal places of all the elements in the array. 1. Use a for loop. The array fArray [ is defined in a file called Lab8Adatasetx.h that will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT