Question

In: Computer Science

C question Using a for-loop: (a) calculate the length of a string, (b) find a word...

C question

Using a for-loop: (a) calculate the length of a string, (b) find a word in a string

Solutions

Expert Solution

C CODE FOR PART (a) AND PART (b):

#include <stdio.h>
// (a) Function to return length of string using a for loop
int length(char str[]){
    int count = 0;
    for(int i = 0; str[i] != '\0'; i++)
        count++;
    return count;
}
// (b) Function to check if word is present in string using for loop
// Return 1 if word is present otherwise 0
int find(char str[], char word[]){
    int strLength = length(str); // Find length of string
    int wordLength = length(word); // Find length of word
    int isFound = 1;
    for(int i = 0; i < strLength - wordLength; i++){
        isFound = 1;
        for(int j = 0; j < wordLength; j++){
            if(str[i + j] != word[j]){    
                isFound = 0;
                break;
            }
        }
        if(isFound)
            return 1;
    }
    return 0;
}
int main(){
    char str[] = "I love programming in C";
    char word1[] = "programming";
    char word2[] = "C++";
    printf("Length of string \"%s\" is %d\n", str, length(str));
    if(find(str, word1))
        printf("\"%s\" found in string\n", word1);
    else
        printf("\"%s\" NOT found in string\n", word1);
    if(find(str, word2))
        printf("\"%s\" found in string\n", word2);
    else
        printf("\"%s\" NOT found in string\n", word2);
    return 0;
}

OUTPUT:

FOR ANY HELP JUST DROP A COMMENT


Related Solutions

By using the string function find to find the index of a space; the function length...
By using the string function find to find the index of a space; the function length to find the length of the string; and the function substr from a file form firstname, middlename, and lastname to lastname, middlename, and first name. Please help me this is c++ thank you
Analyze the motion of a circular pendulum and calculate the length and tension in the string....
Analyze the motion of a circular pendulum and calculate the length and tension in the string. Assume the mass of the pendulum bob is 0.5 kg. the period of the pendulum is 1.19 s. and the radius of the circle is 20 cm, determine the approximate radius of the circle the pendulum sweeps out. Perform a force analysis on the pendulum bob. What is the tension in the string?
Passwords of length 10 are formed using the character set S = {a, b, c, …,...
Passwords of length 10 are formed using the character set S = {a, b, c, …, z, A, B, …, Z, 0, 1, 2, .., 9}. Thus the set S consists of 62 different characters. For each part below, you must include a few words that describe your counting strategy. (a) How many passwords of length 10 use the character Q exactly two times? (b) How many passwords of length 10 use at most one of the “upper case” letters...
VI. Answer part A, B, and C of question 5. 5a) Given a String str1 and...
VI. Answer part A, B, and C of question 5. 5a) Given a String str1 and a String str2, return true if str1 contains any character in str2 anyChar("abc", "xyz") → false anyChar("abc", "xya") → true anyChar("abc", "xyzxyzbxyzxyz") → true 5b) Given a String str1 and a String str2, return true if str1 does not contains any character in str2, and false otherwise. noChar("abc", "xyz") → true noChar("abc", "xya") → false noChar("xyzxyzbxyzxyz", "xyzb") → false 5c) Given a String str1...
// For an input string of words, find the most frequently occurring word. In case of...
// For an input string of words, find the most frequently occurring word. In case of any ties, report any in output. // Your algorithm should be of the runtime O(n) time where n is the number of words in the string. #include <iostream> #include <vector> #include <sstream> using namespace std; string findWord(vector<string>& tokens); int main() {    string line = "FDo all the good you can, by all the means you can,                    in all...
Question 11 mystring.length() or mystring.size() will return the length of the string in mystring. Question 11...
Question 11 mystring.length() or mystring.size() will return the length of the string in mystring. Question 11 options: True False Question 12 To access an element of an array of structures you use the ________ operator. Question 12 options: Dot Bitwise Logical Comparison Question 13 Question 13 options: What would be printed from the following C++ program? #include #include using namespace std; struct Point { int x; int y; }; void makeTriangle(Point p[]); int main() { Point p[3]; int i; makeTriangle(p);...
Sketch a graph of the polar equation and find the length and area of the curve in one loop.
Sketch a graph of the polar equation and find the length and area of the curve in one loop.
True or False: It's easy to loop through an array using a for loop in C++...
True or False: It's easy to loop through an array using a for loop in C++ because you can use the .size() function to get the total number of spaces in the array True or False: It's easy to loop through a vector using a for loop in C++ because you can use the .size() function to get the total number of spaces in the vector
In this C program, the function reverseStr reverses the nth word in a string. If n...
In this C program, the function reverseStr reverses the nth word in a string. If n is larger than the number of words in the string then the function returns 0, else return the function modifies the string and returns 1. Task: write the reverseStr function that follow the requirements below: 1. consistent with the prototype and code below 2. original string must be changed 3. DON'T use functions in string.h Tips: 1. string input to this function is non-empty,...
For C++ Assume that word is a variable of type string that has been assigned a...
For C++ Assume that word is a variable of type string that has been assigned a value. Assume furthermore that this value always contains the letters "dr" followed by at least two other letters. For example: "undramatic", "dreck", "android", "no-drip". Assume that there is another variable declared, drWord, also of type string. Write the statements needed so that the 4-character substring word of the value of word starting with "dr" is assigned to drWord. So, if the value of word...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT