Sponsored Content
Top Forums Programming Counting characters, words, spaces, punctuations, etc. Post 302294682 by jim mcnamara on Thursday 5th of March 2009 04:05:50 PM
Old 03-05-2009
I hope this is not homework.

#include <ctype.h> defines the functions and macros you need.
isspace() spaces (include newlines)
ispunct() punctuation characters
isalnum() regular characters A-z a-z plus digits 0-9

1. open the file with fopen

2 use fgets to read a line

while you get a line of text
3. interate over all of the chacters you just read in - checking char types with those
functions
end while

4. close the file

5. use printf to display all of the results. word count per line == # of isspace() characters you find on the line, so it also equals the total words. This assumes only single spaces between characters. And no leading spaces on a line.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Counting words in a file

I'm trying to figure out a way to count the number of words in the follwing file: cal 2002 > file1 Is there anyway to do this without using wc but instead using the cut command? (1 Reply)
Discussion started by: r0mulus
1 Replies

2. UNIX for Dummies Questions & Answers

Help needed counting spaces

I was wondereing if somebody could point me in the right direction - below is a small script I have put together to check that lines in a file I recieve are the correct length (371) and to output any incorrect lines to a .txt file. However when I run this it does not seem to count spaces as... (3 Replies)
Discussion started by: MarBergi
3 Replies

3. UNIX for Dummies Questions & Answers

counting words then amending to a file

i want to count the number of words in a file and then redirect this to a file echo 'total number of words=' wc -users>file THis isnt working, anyone any ideas. (1 Reply)
Discussion started by: iago
1 Replies

4. UNIX for Dummies Questions & Answers

counting words

if i have a long list of data, with every line beginning with an ip-address, like this: 62.165.8.187 - - "GET /bestandnaam.html HTTP/1.1" 200 5848 "http://www.domeinnaam.nl/bestandnaam.html" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)" how do i count which ip-adresses are mentioned... (3 Replies)
Discussion started by: FOBoy
3 Replies

5. Shell Programming and Scripting

Counting words

Hi Is there a way to count the no. of words in all files in directory. All are text files.I use wc -w but somehow i am not getting the rite answer. Is there an alternative. Thanks in advance (9 Replies)
Discussion started by: kinny
9 Replies

6. Programming

Counting the words in a file

Please find the below program. It contains the purpose of the program itself. /* Program : Write a program to count the number of words in a given text file */ /* Date : 12-June-2010 */ # include <stdio.h> # include <stdlib.h> # include <string.h> int main( int argc, char *argv ) {... (6 Replies)
Discussion started by: ramkrix
6 Replies

7. Shell Programming and Scripting

Help in counting the no of repeated words with count in a file

Hi Pls help in solving my doubt.Iam having file like below file1.txt priya jenny jenny priya raj radhika priya bharti bharti Output required: I need a output like count of repeated words with name for ex: priya 3 jenny 2 (4 Replies)
Discussion started by: bha148
4 Replies

8. Shell Programming and Scripting

counting words with perl?

how to use perl to count number of lines, words characters in a file. (3 Replies)
Discussion started by: winter9
3 Replies

9. Shell Programming and Scripting

Counting occurrence of all words in a file

Hi, Given below is the input file: http://i53.tinypic.com/2vmvzb8.png Given below is what the output file should look like: http://i53.tinypic.com/1e6lfq.png I know how to count the occurrence of 1 word from a file, but not all of them. Can someone help please? An explanation on the... (1 Reply)
Discussion started by: r4v3n
1 Replies

10. Shell Programming and Scripting

Counting specific words from the log

Hi, I need a shell script which can provide details from error logs like this Aug 23 21:19:41 red mountd: authenticated mount request from bl0110.bang.m pc.local:651 for /disk1/jobs (/disk1) Aug 23 08:49:52 red dhcpd: DHCPDISCOVER from 00:25:90:2b:cd:7c via eth0: unknown client Aug 24... (2 Replies)
Discussion started by: ratheeshp
2 Replies
ctype(3C)																 ctype(3C)

NAME
ctype, isalpha, isalnum, isascii, isblank, iscntrl, isdigit, islower, isprint, isspace, isupper, ispunct, isgraph, isxdigit - character handling SYNOPSIS
#include <ctype.h> int isalpha(int c); int isalnum(int c); int isascii(int c); int isblank(int c); int iscntrl(int c); int isdigit(int c); int isgraph(int c); int islower(int c); int isprint(int c); int ispunct(int c); int isspace(int c); int isupper(int c); int isxdigit(int c); These macros classify character-coded integer values. Each is a predicate returning non-zero for true, 0 for false. The behavior of these macros, except isascii(), is affected by the current locale (see setlocale(3C)). To modify the behavior, change the LC_TYPE category in setlocale(), that is, setlocale(LC_CTYPE, newlocale). In the "C" locale, or in a locale where character type information is not defined, characters are classified according to the rules of the US-ASCII 7-bit coded character set. The isascii() macro is defined on all integer values. The rest are defined only where the argument is an int, the value of which is repre- sentable as an unsigned char, or EOF, which is defined by the <stdio.h> header and represents end-of-file. Functions exist for all the macros defined below. To get the function form, the macro name must be undefined (for example, #undef isdigit). For macros described with Default and Standard conforming versions, standard-conforming behavior is provided for standard-conforming appli- cations (see standards(5)) and for applications that define __XPG4_CHAR_CLASS__ before including <ctype.h>. Default isalpha() Tests for any character for which isupper() or islower() is true. Standard conforming isalpha() Tests for any character for which isupper() or islower() is true, or any character that is one of the current locale- defined set of characters for which none of iscntrl(), isdigit(), ispunct(), or isspace() is true. In "C" locale, isalpha() returns true only for the characters for which isupper() or islower() is true. isalnum() Tests for any character for which isalpha() or isdigit() is true (letter or digit). isascii() Tests for any ASCII character, code between 0 and 0177 inclusive. isblank() Tests whether c is a character of class blank in the current locale. This macro/function is not available to applications conforming to standards prior to SUSv3. See standards(5) iscntrl() Tests for any ``control character'' as defined by the character set. isdigit() Tests for any decimal-digit character. Default isgraph() Tests for any character for which ispunct(), isupper(), islower(), and isdigit() is true. Standard conforming isgraph() Tests for any character for which isalnum() and ispunct() are true, or any character in the current locale-defined "graph" class which is neither a space ("") nor a character for which iscntrl() is true. islower() Tests for any character that is a lower-case letter or is one of the current locale-defined set of characters for which none of iscntrl(), isdigit(), ispunct(), isspace(), or isupper() is true. In the "C" locale, islower() returns true only for the characters defined as lower-case ASCII characters. Default isprint() Tests for any character for which ispunct(), isupper(), islower(), isdigit(), and the space character ("") is true. Standard conforming isprint() Tests for any character for which iscntrl() is false, and isalnum(), isgraph(), ispunct(), the space character (""), and the characters in the current locale-defined "print" class are true. ispunct() Tests for any printing character which is neither a space ("") nor a character for which isalnum() or iscntrl() is true. isspace() Tests for any space, tab, carriage-return, newline, vertical-tab or form-feed (standard white-space characters) or for one of the current locale-defined set of characters for which isalnum() is false. In the "C" locale, isspace() returns true only for the standard white-space characters. isupper() Tests for any character that is an upper-case letter or is one of the current locale-defined set of characters for which none of iscntrl(), isdigit(), ispunct(), isspace(), or islower() is true. In the "C" locale, isupper() returns true only for the characters defined as upper-case ASCII characters. Default isxdigit() Tests for any hexadecimal-digit character ([0-9], [A-F], or [a-f]). Standard conforming isxdigit() Tests for any hexadecimal-digit character ([0-9], [A-F], or [a-f] or the current locale-defined sets of characters repre- senting the hexadecimal digits 10 to 15 inclusive). In the "C" locale, only 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f are included. If the argument to any of the character handling macros is not in the domain of the function, the result is undefined. Otherwise, the macro or function returns non-zero if the classification is TRUE and 0 if the classification is FALSE. USAGE
These macros or functions can be used safely in multithreaded applications, as long as setlocale(3C) is not being called to change the locale. See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | |CSI |Enabled | |Interface Stability |Standard | |MT-Level |MT-Safe with exceptions | +-----------------------------+-----------------------------+ setlocale(3C), stdio(3C), ascii(5), environ(5), standards(5) 28 Jan 2005 ctype(3C)
All times are GMT -4. The time now is 12:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy