Ans)
this is counting using ascii encoding from 'a'-'z' i.e 26 charctors are their
assuming alphabet[0] is counting for 'a' likewise others a[25] counting for 'z'
in below function we are reducing current char i.e str[i] - 'a' it will give the
difference between 'a' and given char which is equal to char in array
for example :
assume current chars is c then, 'c'-'a' =2 in ascii and in alphabet[2] will be incremented
alphabet[0] counting for a alphabet[1] for b and alphabet[2] for c
this is way its working
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int count[26]; // to store frequency of every alphabet
char* toCharArray(string out){ //to convert output string to char array
char* array;
array = new char[out.size() + 1];
for (int i = 0; i < out.size(); i++)
{
array[i] = out[i];
}
return array;
}
int main() { // main function
string filename,outfile;
cout << "Enter File name:";
cin >> filename;
cout << "Enter Full name and path of outfile:";
cin >> outfile;
ifstream file;
file.open((filename.c_str())); //if file does not exist
if (!file)
{
cout << "Cannot Open file";
return 0;
}
string out,temp;
out = "";
while (!file.eof()) //reading all charctors from file
{
getline(file,temp);
out.append(temp);
}
file.close();
char *str = toCharArray(out);
//please read this for better understanding
/*
this is counting using ascii encoding from a-z 26 charctors are their
assuming alphabet[0] is counting for 'a' likewise others a[25] counting for 'z'
in below function we are reducing current char i.e str[i] - 'a' it will give the
difference between 'a' and given char which is equal to char in array
for example :
assume current chars is c then, 'c'-'a' =2 in ascii and in alphabet[2] will be incremented
alphabet[0] counting for a alphabet[1] for b and alphabet[2] for c
this is way its working
*/
int i = 0, alphabet[26] = {0}, j;
while (str[i] != '\0') { //counting frequency of every alphabet
if (str[i] >= 'a' && str[i] <= 'z') {
j = str[i] - 'a';
++alphabet[j];
}
++i;
}
ofstream output;
output.open(outfile.c_str()); //opening output file
for (i = 0; i < 26; i++)
{
if (alphabet[i] == 0) //we are skipping char whose count is zero
{ //we will not put them into file
continue;
}
output << char(i + 'a') << " " << alphabet[i] << "\n";
}
return 0;
}
hi, please help in c++. I dont understand how to do this, and a lot of...
C++ Programming question
Problem: 5. Write a program that reads in a list of integers into an array with base type int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input, the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is to be...
I have written a program that determines the frequency of letters in an input file. It works perfectly but the only problem is that the output file should put the characters in the order that they appear but they are being put in alphabetical order. Please help. Here is the code: #include <fstream> #include <iostream> using namespace std; int main() { ofstream out; out.open("output.txt"); ifstream input("input.txt", ios_base::binary); size_t count[256]; fill_n(count, 256, 0); for (char c; input.get(c); ++count[uint8_t(c)]) ; for (size_t...
Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...
//I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...
Programming Exercise 4.9
Write a script named numberlines.py. This script
creates a program listing from a source program.
This script should:
Prompt the user for the names of two files.
The input filename could be the name of the script itself, but
be careful to use a different output filename!
The script copies the lines of text from the input file to the
output file, numbering each line as it goes.
The line numbers should be right-justified in 4 columns,...
Please write this in C.
Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...
C++ Write a program that reads students’ names followed by their test scores from the given input file. The program should output to a file, output.txt, each student’s name followed by the test scores and the relevant grade. Student data should be stored in a struct variable of type StudentType, which has four components: studentFName and studentLName of type string, testScore of type int and grade of type char. Suppose that the class has 20 students. Use an array of...
This assignment tests your ability to write C programs that handle keyboard input, formatted console output, and input/output with text files. The program also requires that you use ctype functions to deal with the logic. In this program, you will input from the user two characters, which should both be letters where the second letter > the first letter. You will then input a file character-by-character and output those letters that fall between the two characters (inclusively) to an output...
need help on C++ Discussion: The program should utilize 3 files. player.txt – Player name data file – It has: player ID, player first name, middle initial, last name soccer.txt – Player information file – It has: player ID, goals scored, number of penalties, jersey number output file - formatted according to the example provided later in this assignment a) Define a struct to hold the information for a person storing first name, middle initial, and last name). b) Define...
In C++ program a simple k-means clustering algorithm, kmeans, using the Euclidean distance for 2-dimensional numerical data. Your program should be executed as follows: kmeans k input.txt where input parameter k > 1 is an integer, specifying the number of clusters. input.txt is an input file containing many 2-dimensional data points in the following format, 274 119 317 144 267 164 233 137 272 99 297 116 268 142 522 286 468 308 441 263 Your program should output a...