Sponsored Content
Top Forums Programming Simple C program to count word lengths Post 302983536 by Riker1204 on Wednesday 12th of October 2016 05:56:00 PM
Old 10-12-2016
Simple C program to count word lengths

So my program is not working and I keep changing it to figure out why. So I have two questions, can I do tracing similar to bash, and also what is wrong with this.

The idea is simple, I want to count "word" lengths, with the loose definition of word not being a space, tab, or newline. Here is what I wrote to test if it is counting words by outputting the number of 3 letter words, but it always gives 0.

Code:
#include <stdio.h>

main() {

     int c, i, state, wl;
     int maxlength[15];

     #define IN 1
     #define OUT 0

     wl = i = c = 0;
     state = OUT;
    
     for (i=0; i < 15; ++i)
          maxlength[i] = 0;

     while ((c = getchar()) != EOF) {
          if (c == ' ' || c == '\t' || c == '\n') {
               if (state == OUT)
                    ;
               else if (state == IN) {
                    state == OUT;
                    ++maxlength[wl];
                    wl = 0;
               }
          else { 
               ++wl;
               state = IN;
          }
          }
     }
     printf("Number of 3 letter words: %d\n", maxlength[3]); 
}

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do I count # of char. in a word?

I havent done shell scripting in quite some time. I want to know how to count the number of characters in a word, specifically a parameter. Example: myscript hello I want "myscript" to return the number of charcaters in the parameter "hello". Any ideas? (9 Replies)
Discussion started by: xadamz23
9 Replies

2. UNIX for Dummies Questions & Answers

count word

hi, given a file i need to get the first line and secodn line and count each of the line whether the length of first line and second line is the same i don;t know how to get the length of the line...seems like use 'wc' cannot do it... please advice (1 Reply)
Discussion started by: ariuscy
1 Replies

3. Shell Programming and Scripting

specified word count

hi iam trying to do a specified word count on file called text i have a few ideas but don't get the result i want do any one have a idea please help i have this at the moment cat text echo "Please enter the word you are looking for:" read string echo "the word < $string > occurs in... (5 Replies)
Discussion started by: bhaviknp
5 Replies

4. UNIX for Dummies Questions & Answers

Word Count

Hi All, Please let me know how to get the count of a particular word in a file. Example. I am looking for count of word 'result' in a file abc.xml. Thanks, Shankar (10 Replies)
Discussion started by: s_chowhan
10 Replies

5. Fedora

word count wc

could someone explain this please. echo aaaa|wc -c 5 echo aaaa|wc -m 5 But I'd expect the count to be 4 Its SunOS 5.8 Thanks in Advance. (5 Replies)
Discussion started by: chaandana
5 Replies

6. Shell Programming and Scripting

Word count of lines ending with certain word

Hi all, I am trying to write a command that can help me count the number of lines in the /etc/passwd file ending in bash. I have read through other threads but am yet to find one indicating how to locate a specifc word at the end of a line. I know i will need to use the wc command but when i... (8 Replies)
Discussion started by: warlock129
8 Replies

7. UNIX for Advanced & Expert Users

Word count

Script that lists all words used in one or more files and displays their count (pattern /\W+/ to split the lines of the input file into words can b used).. It should display list in format word:count...gets Filename as an cmd line argument! eg: $perl test doc (where doc is d file we are going to... (4 Replies)
Discussion started by: aadi_uni
4 Replies

8. Shell Programming and Scripting

if, word count

Hi, I need to count the lines of a file stack.html and if the amount lines i want to do something. At this moment, I have if ; then ... This is not working. Any ideas? Thanks! (3 Replies)
Discussion started by: azertyazerty
3 Replies

9. Shell Programming and Scripting

Word Count error

I need to read a trigger file whose name can be: ABC!DEF@2014.txt or ABC!DEF@2014,2015.txt and then carry out functions on those inputs. Currently I am doing: YEAREXPORT { FILE= xyz.txt ls ABC* -l > ${FILE} if ; then log_err "Trigger File ABC* does not exist!" fi (4 Replies)
Discussion started by: rajiv_kool
4 Replies

10. UNIX for Beginners Questions & Answers

UNIX script to check word count of each word in file

I am trying to figure out to find word count of each word from my file sample file hi how are you hi are you ok sample out put hi 1 how 1 are 1 you 1 hi 1 are 1 you 1 ok 1 wc -l filename is not helping , i think we will have to split the lines and count and then print and also... (4 Replies)
Discussion started by: mirwasim
4 Replies
curl_printf(3)							  libcurl Manual						    curl_printf(3)

NAME
curl_maprintf, curl_mfprintf, curl_mprintf, curl_msnprintf, curl_msprintf curl_mvaprintf, curl_mvfprintf, curl_mvprintf, curl_mvsnprintf, curl_mvsprintf - formatted output conversion SYNOPSIS
#include <curl/mprintf.h> int curl_mprintf(const char *format, ...); int curl_mfprintf(FILE *fd, const char *format, ...); int curl_msprintf(char *buffer, const char *format, ...); int curl_msnprintf(char *buffer, size_t maxlength, const char *format, ...); int curl_mvprintf(const char *format, va_list args); int curl_mvfprintf(FILE *fd, const char *format, va_list args); int curl_mvsprintf(char *buffer, const char *format, va_list args); int curl_mvsnprintf(char *buffer, size_t maxlength, const char *format, va_list args); char *curl_maprintf(const char *format, ...); char *curl_mvaprintf(const char *format, va_list args); DESCRIPTION
These are all functions that produces output according to a format string and given arguments. These are mostly clones of the well-known C- style functions and there will be no detailed explanation of all available formatting rules and usage here. See this table for notable exceptions. curl_mprintf() Normal printf() clone. curl_mfprintf() Normal fprinf() clone. curl_msprintf() Normal sprintf() clone. curl_msnprintf() snprintf() clone. Many systems don't have this. It is just like sprintf but with an extra argument after the buffer that specifies the length of the target buffer. curl_mvprintf() Normal vprintf() clone. curl_mvfprintf() Normal vfprintf() clone. curl_mvsprintf() Normal vsprintf() clone. curl_mvsnprintf() vsnprintf() clone. Many systems don't have this. It is just like vsprintf but with an extra argument after the buffer that specifies the length of the target buffer. curl_maprintf() Like printf() but returns the output string as a malloc()ed string. The returned string must be free()ed by the receiver. curl_mvaprintf() Like curl_maprintf() but takes a va_list pointer argument instead of a variable amount of arguments. To easily use all these cloned functions instead of the normal ones, #define _MPRINTF_REPLACE before you include the <curl/mprintf.h> file. Then all the normal names like printf, fprintf, sprintf etc will use the curl-functions instead. RETURN VALUE
The curl_maprintf and curl_mvaprintf functions return a pointer to a newly allocated string, or NULL it it failed. All other functions return the number of character they actually outputed. SEE ALSO
printf(3), sprintf(3), fprintf(3), vprintf(3) libcurl 7.7.2 20 April 2001 curl_printf(3)
All times are GMT -4. The time now is 10:06 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy