Thread: Targeting specific characters in unknown array

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    7

    Arrow Targeting specific characters in unknown array

    Firstly I just want to say that I don't want any solution or anything that will even mean that I don't have to do some extra thinking. I'm just looking for a nod in the right direction.

    I'm trying to implement a readability programme where I firstly need to prompt user for input and print the number of letters, words and sentences.


    I've successfully completed the "letters" element using a for loop.

    Code:
    for (int c = 0, n = strlen(text); c < n; c++)
    {
            if (isalpha(text[c]))
            {
               count_letters++;
            }
    }

    Now need to do the "words". I've tried lots of different things but had no luck.

    For the purposes of this program, a word may be defined as the first character in a string, passing over the other characters until a *whitespace* which denotes a new word.

    I believe that what I need to do is target specific elements in the array and test them using a function like

    Code:
    isalpha
    and/or
    Code:
    isspace
    However, since the array is created by the user's input, I simply won't be able, in advance, to know the length of the array.

    The following code:

    Code:
    for (int d = 0, e = strlen(text); d < e; d++)
             {
                printf("%c\n", text[0]);
                printf("%c\n", text[4]);
                count_words++;
             }
    prints the first two letters (n and y) of the words I typed in (not you), so I know that the first letters exist at those points.

    However, when I run:

    Code:
    for (int d = 0, e = strlen(text); d < e; d++)
             {
               if (isalpha(text[0]))
               {
                  count_words++;
               }
             }
    it prints "7 word(s)" (6 letters + 1 whitespace).

    Sorry for long post!

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    421
    The way to approach this is to iterate though the whole string, one character at a time. Then you maintain a state variable, "on word" or "off word", depending on whether you are in a word or not. Each time you transition from the "on word" to "off word" state, that's a word, so you increment the word count.
    You will have to handle the terminating nul, and you might need special logic for pathological cases like apstrophes at the beginnings of letter sequences.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to print specific amount of characters per line
    By Yashiiro in forum C Programming
    Replies: 4
    Last Post: 06-13-2016, 11:52 AM
  2. Replies: 13
    Last Post: 04-02-2012, 05:51 PM
  3. Ignoring specific characters in a string
    By seanksg in forum C Programming
    Replies: 11
    Last Post: 05-02-2011, 06:08 PM
  4. Identifying specific characters in a string
    By London Fog in forum C Programming
    Replies: 8
    Last Post: 08-14-2009, 12:59 PM
  5. Removing Specific Characters from Strings
    By gfmartin05 in forum C++ Programming
    Replies: 4
    Last Post: 02-09-2009, 09:53 AM

Tags for this Thread