Visit Our UNIX and Linux User Community


output the letters of the alphabet with the number of occurrences


 
Thread Tools Search this Thread
Top Forums Programming output the letters of the alphabet with the number of occurrences
# 1  
Old 12-02-2007
output the letters of the alphabet with the number of occurrences

hi,
I'm trying to create a program that will read a file and then check the file for each letter of the alphabet and then output the letter and the number of times it appears in the file, into a new file... this is what i have so far but it's not working.. if anyone could help that would be nice!
thanks

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;

int main()
{
cout << "\n To find the number of occurrences of each letter in the alphabet in a specific file,"
<< "enter the file name:";
string inFile;
cin >> inFile;

cout << "\nEnter the name of the output file:";
string outFile;
cin >> outFile;

ifstream fin(inFile.data());
assert (fin.is_open());
ofstream fout(outFile.data());
assert (fout.is_open());

vector<int> numoccurrences(26);
int i;
char letter;
for(;Smilie
{
fin >> letter;
char lowercase = tolower(letter);
if ('a'<= lowercase <='z')
numoccurrences[ lowercase - 'a']++;
}
char ch = 0;
for (int i = 1; i < numoccurrences.size(); i++)
{
fout << ch << numoccurrences[i];
ch++;
}
}
# 2  
Old 12-02-2007
moved to a more appropriate forum - 'High Level Programming'
# 3  
Old 12-02-2007
i really need some help with this.. if anyone has any ideas at all please post
# 4  
Old 12-02-2007
Is this one of those homework questions?
# 5  
Old 12-02-2007
it is for homework, but i dont want people to give me the answer, just a clue about where it's messing up, because i have no clue
# 6  
Old 12-02-2007
I like the use of C++ to make a simple problem more convoluted.

Code:
#include <stdio.h>

int main(int argc,char **argv) {
static int occur[256];
int i=' '; 

	while (1) {
		int c=getchar();
		if (c==EOF) break;
		occur[(unsigned char)c]++;
	}

	while (i < 127) {
		if (occur[i]) printf("%c: %d\n",i,occur[i]);
		i++;
	}

	return 0;
}


Last edited by porter; 12-02-2007 at 07:36 PM..
# 7  
Old 12-02-2007
i'm not really sure what alot of that stuff means.. i'm new to C++... i don't know what arcg and **argv mean.. also is this just for a paragraph of 256 letters? and why do you assign int i = 32? and i also dont know what return 0 does...

Previous Thread | Next Thread
Test Your Knowledge in Computers #220
Difficulty: Easy
When multiple routers are used in interconnected networks, the routers can exchange information about destination addresses using a routing protocol.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to strictly grep (with before and after args) for last N number of occurrences?

Here is my grep string to print only the last occurrence, with before and after lines. Note that the tail Argument is sum of B and A args + 1, so that it prints the data of only the last 1 match. Now I need to print last 2 such matches. I thought of doubling the tail arg like 5+5+1 (For -- line),... (2 Replies)
Discussion started by: samjna
2 Replies

2. Shell Programming and Scripting

Count the number of string occurrences to display 0 entries in output

Hello Friends, Can somebody assist an issue I am having? I have a separate file with a list of account ids XXX200B02Y01 XXX200B03Y01 XXX200B05Y01 XXX200B07Y01 XXX200B08Y01 I call the file, and run an egrep against a directory and logfiles AccountID=$(cat... (2 Replies)
Discussion started by: liketheshell
2 Replies

3. Shell Programming and Scripting

Help with Unix and Awk to count number of occurrences

Hi, I have a file (movies.sh), this file contains list of movies such as I want to redirect the movies from movies.sh to file_to_process to allow me process the file with out losing anything. I have tried Movies.sh >> file_to_process But I want to add the row number to the data... (2 Replies)
Discussion started by: INHF
2 Replies

4. Shell Programming and Scripting

only a number can be entered no letters?

ok the user can only enter a number if a letter is entered it shouldnt be accepted This is what i have so far read -p "How many cars to enter:" cars until do read -p "Invalid number. Please re-enter:" $tags done (5 Replies)
Discussion started by: gangsta
5 Replies

5. Linux

How to sort the number of occurrences

file:///C:/Users/TSHEPI%7E1.LEB/AppData/Local/Temp/moz-screenshot.pngATM@ubuntu:~$ cat numbers2 | sort -n | uniq -c 1 7 1 11 2 10 3 the 1st numbers are the counts from the command "uniq -c", which represent the number of occurrences of each in the file. The "sort -n"... (4 Replies)
Discussion started by: lebogot
4 Replies

6. Shell Programming and Scripting

counting number of pattern occurrences

Hi All, Is it possible to count number of occurrences of a pattern in a single record using awk?? for example: a line like this: abrsjdfhafa I want to count the number of a character occurrences. but still use the default RS, I don't want to set RS to single character. (1 Reply)
Discussion started by: ghoda2_10
1 Replies

7. UNIX for Dummies Questions & Answers

Print number of occurrences of many different strings

People, I need your help with making a script which will 1. take as an input the number of lines, smth like this: ((RUBROBACTER_1_PE1288 (((SALINISPORA_1_PE1863 SALINISPORA_1_PE1828)100 ((NOCARDIOIDES_2_PE2419 PROPIONIBACTERIUM_1_PE1395)96 ((((((((CORYNEBACTERIUM_1_PE1119... (3 Replies)
Discussion started by: roussine
3 Replies

8. Shell Programming and Scripting

Count the number of occurrences of the word

I am a newbie in UNIX shell script and seeking help on this UNIX function. Please give me a hand. Thanks. I have a large file. Named as 'MyFile'. It was tab-delmited. I am told to write a shell function that counts the number of occurrences of the ord “mysring” in the file 'MyFile'. (1 Reply)
Discussion started by: duke0001
1 Replies

9. UNIX for Dummies Questions & Answers

checking wether an input is using letters of the alphabet

afternoon forums. I need to get a way of testing as to wether an inputed character is part of the english alphabet. i have come up with the following code but its not working at all. until '] do echo This is not a Letter done any help would be beneficial to me. (1 Reply)
Discussion started by: strasner
1 Replies

10. Shell Programming and Scripting

To check if the first character is a alphabet or number

Hi, I need to find whether the first character in a line is a alphabet or a number. If its a number i should sort it numerically. If its a alphabet i should sort it based on the ASCII value.And if it is something other than alphabet or number then sort it based on ASCII value. The code i used... (2 Replies)
Discussion started by: ragavhere
2 Replies

Featured Tech Videos