Sponsored Content
Top Forums Programming output the letters of the alphabet with the number of occurrences Post 302148503 by porter on Sunday 2nd of December 2007 05:54:02 PM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
subgetopt(3)						     Library Functions Manual						      subgetopt(3)

NAME
subgetopt - get option character from command line SYNTAX
#include <subgetopt.h> char *sgoptarg; int sgoptind; int sgoptpos; int sgoptdone; int sgoptproblem; int sgopt(argc,argv,opts); int argc; char **argv; char *opts; DESCRIPTION
sgopt returns the next valid command-line option character from argv. Valid option characters are listed in the opts string. opts may be empty. A character in opts may be followed by a colon, in which case it takes an option argument. Avoid using the characters ?, :, and - as option characters. Below option argument is abbreviated as optarg and command-line argument is abbreviated as cmdarg. Options are listed in cmdargs which begin with a minus sign. Several options which do not take optargs may be combined into one cmdarg. An option which takes an optarg may be handled in two ways. If it appears at the very end of a cmdarg, then the entire next cmdarg is the optarg. But if there are any characters in the cmdarg after the option character, then those characters form the optarg. The optarg is returned in sgoptarg. Next time sgopt looks at the cmdarg which follows the optarg. If a cmdarg does not begin with a hyphen, or if it is a lone hyphen not followed by any characters, or if it begins with two hyphens, then it terminates option processing, and sgopt returns an appropriate code. If there are two hyphens, sgopt will advance attention to the next cmdarg, so it can be called again to read further options. PROPER USAGE
sgoptproblem should be used only when sgopt returns ?. sgoptind and sgoptpos are defined all the time. sgoptarg is defined all the time; it is null unless sgopt has just returned an option with optarg. sgopt is typically used as follows. #include <subgetopt.h> main(argc,argv) int argc; char **argv; { int opt; while ((opt = sgopt(argc,argv,"a:s")) != sgoptdone) switch(opt) { case 'a': printf("opt a with optarg %s ",sgoptarg); break; case 's': printf("opt s with no optarg "); break; case '?': if (argv[sgoptind] && (sgoptind < argc)) printf("illegal opt %c ",sgoptproblem); else printf("missing arg, opt %c ",sgoptproblem); exit(1); } argv += sgoptind; while (*argv) printf("argument %s ",*argv++); exit(0); } The end of the command line is marked by either argc, or a null pointer in argv, whichever comes first. Normally these two markers coin- cide, so it is redundant to test for both argv[sgoptind] and sgoptind < argc. The above code shows both tests as an illustration. Multiple option sets: One useful technique is to call sgopt with a primary opts until it returns EOF, then call sgopt with a secondary opts until it returns EOF. The user can provide primary options, then a double hyphen, and then secondary options. No special handling is needed if some or all of the options are omitted. The same technique can be used for any number of option sets in series. Multiple command lines: Before parsing a new argv, make sure to set sgoptind and sgoptpos back to 1 and 0. PARSING STAGES
sgopt keeps track of its position in argv with sgoptind and sgoptpos, which are initialized to 1 and 0. It looks at argv[sgoptind][sgopt- pos] and following characters. sgopt indicates that no more options are available by returning sgoptdone, which is initialized to SUBGETOPTDONE, which is defined as -1. sgopt begins by setting optarg to null. Ending conditions: If argv is null, or sgoptind is larger than argc, or the current cmdarg argv[sgoptind] is null, then sgopt returns opt- done. Stage one: If the current character is zero, sgopt moves to the beginning of the next cmdarg. It then checks the ending conditions again. Stage two: If the current position is the begining of the cmdarg, sgopt checks whether the current character is a minus sign. If not it returns optdone. It then moves to the next character. If that character is zero, sgopt moves back to the beginning of the cmdarg, and returns sgoptdone. If the character is a minus sign, sgopt moves to the beginning of the next cmdarg, and returns sgoptdone. Stage three: sgopt records the current character, c, and moves to the next character. There are three possibilities: (1) c is an option character without optarg in opts, or (2) c is an option character with optarg in opts, or (3) c does not appear in opts. (1) If c appears as an option character without optarg in opts, sgopt returns c. (2) If c appears as an option character with optarg in opts, sgopt sets sgoptarg to the current position, and moves to the next cmdarg. If sgoptarg is nonempty, sgopt returns c. Then sgopt sets sgoptarg to the current cmdarg. If the current cmdarg is null, or past argc, sgopt sets sgoptproblem to c and returns ?. Otherwise sgopt moves to the next argument and returns c. (2) If c does not appear in opts, sgopt sets sgoptproblem to c and returns ?. SYNTAX NOTE
sgopt is actually a macro abbreviation for subgetopt. The external sg variables are also macros for subget. These macros are defined in <subgetopt.h>, unless SUBGETOPTNOSHORT is defined when <subgetopt.h> is included. VERSION
subgetopt version 0.9, 931129. AUTHOR
Placed into the public domain by Daniel J. Bernstein. subgetopt(3)
All times are GMT -4. The time now is 12:01 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy