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:
String Processing Labs Directions: Write a main program to test the three functions described below, Input...
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, 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 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 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. 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 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 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 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 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 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...