Question

String Processing Labs Directions: Write a main program to test the three functions described below, Input is from a file, and output is to a file. 1. The function Another parameter is a char variable with a letter value. The function outputs the number of times the char value appears in the string. processes a string containing letters of the alphabet, which is a parameter 2. This Boolean function has two string parameters. It returns true when the first str substring of the second string, otherwise it returns false. ing is a 3. The input is on one line in an input file, and has up to seven components with a colon ) at end of each component. The function separates each component into separate strings (without the colon), and outputs each separated string along with its length. In the output file, use one line for each separated string. One input sample could be George:April:May: William:Johnny: Another input could be Red:Fuscia:Magenta: A third input example is (note intemal spaces) Hello, there..How are you?:Im fine. Lets go out to dinner. e pare tauses
0 0
Add a comment Improve this question Transcribed image text
Answer #1

C++ Program:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

//Function that counts number of characters present in a string
int charCount(string str, char ch)
{
    int i, cnt=0;

    //Iterating over each character
    for(i=0; i<str.size(); i++)
    {
        //Checking character
        if(str[i] == ch)
            cnt+=1;
    }

    //Returning count
    return cnt;
}

//Function that checks whether first string is a substring of another string
bool isSubstring(string str1, string str2)
{
    int s1Len = str1.length();
    int s2Len = str2.length();
    int i, j;

    // Iterating over string
    for (i = 0; i <= s2Len - s1Len; i++)
    {
        // Checking pattern
        for (j = 0; j < s1Len; j++)
        {
            //Comparing strings
            if (str2[i + j] != str1[j])
                break;
        }

        //Checking length
        if (j == s1Len)
            return true;
    }

    return false;
}

//Function that process input file
void processFile()
{
    string s;

    //Opening files
    fstream fin("ipFile.txt", ios::in);
    fstream fout("opFile.txt", ios::out);

    //Iterating over line by line
    while(fin.good())
    {
        getline(fin, s);
        string delimiter = ":";

        size_t pos = 0;

        string token;

        //Splitting line
        while ((pos = s.find(delimiter)) != std::string::npos)
        {
            token = s.substr(0, pos);
            fout << token << " " << token.size() << endl;
            s.erase(0, pos + delimiter.length());
        }
    }

    //Closing files
    fin.close();
    fout.close();
}

//Main function
int main()
{
    string str1="hello", str2="world", str3="ell";

    cout << "\n String 1: " << str1;
    cout << "\n String 2: " << str2;
    cout << "\n String 3: " << str3;

    //Counting function
    cout << "\n\n charCount(str1, 'l'): " << charCount(str1, 'l') << "\n";

    //Checking substring
    cout << "\n isSubstring(str3, str1): " << isSubstring(str3, str1);
    cout << "\n isSubstring(str2, str1): " << isSubstring(str2, str1);

    //Processing file
    processFile();
    cout << "\n\n File processed Successfully... \n";

    cout << endl;
    return 0;
}

________________________________________________________________________________

Sample Run:

CTC StringProcesslbin\DebugStringProcess.exe String 1: hello String 2: world String 3 ell charCount (str1, 1) 2 ipFile.txt - Notepad File Edit Format View Help isSubstring (str3, str1): 1 isSubstring(str2, str1): 0 ed:Blue:Green: One:Two:Three: File processed Successfully... Process returned 0 (0x) execution time 0.140 s Press any key to continue. opFile.txt Notepad File Edit Format View Help Red 3 Blue 4 Green 5 One 3 wo Three 5

Add a comment
Know the answer?
Add Answer to:
String Processing Labs Directions: Write a main program to test the three functions described below, Input...
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
  • You need to write a program (one java class containing Main calling function isPalindrome (String str)....

    You need to write a program (one java class containing Main calling function isPalindrome (String str). The function isPalindrome (returns Boolean T/F) needs to determine whether or not a string is a palindrome, using recursion. The algorithm to check whether a string is a palindrome is shown below: /* check for first and last char of String: * if they are same then do the same thing for a substring * with first and last char removed. and carry on...

  • Write a Python function called more() that takes three string inputs and outputs a string Formally,...

    Write a Python function called more() that takes three string inputs and outputs a string Formally, the function signature and output are given by rucharist, char: str words str) > str Use the same names for the input arguments as shown above Note that charl and char2 will always be a string of length 1 (ie, it is a single character. The function checks which of charl or char2 appears more often in the word string and returns that character...

  • Write a program that can remove spaces from an input string, find the indexes of a...

    Write a program that can remove spaces from an input string, find the indexes of a character within the string and replace that character with another character. Here is an example input: I am an input string a b The first line, "I am an input string" represents the input string. Please put it into a string variable using getline. In the second line "a b", a is the character that needs to be located within the input string, and...

  • C programming Write the implementation for the three functions described below. The functions are called from...

    C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. a) Write a print_string function that prints the characters in a string to screen on- by-one. b) Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • In C programming Write the implementation for the three functions described below. The functions are called...

    In C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. Write a print_string function that prints the characters in a string to screen on- by-one. Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1 if...

  • Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line...

    Write a C++ program that repeatedly reads lines until an EOF is encountered. As each line is read, the program strips out all characters that are not upper or lower case letters or spaces, and then outputs the line. Thus, the program acts as a filter and issues no prompt. There are many ways this program could be written, but to receive full credit, you must observe the following: Place your code in a file called filterChars.cpp. The program should...

  • Write a complete program that uses the functions listed below. Except for the printOdd function, main...

    Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function calls. Be sure to read in data from the input file. Using the input file provided, run your program to generate an output file. Upload the output file your program generates. •Write a...

  • Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

  • Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

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