C Language: Count the number of letters and words in the English sentence of the input line, with annotations!

Source: Internet
Author: User

Through the keyboard input a line of English sentences, statistics of the number of English letters and words, words separated by a space (punctuation is not counted words);
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Main ()
{
Char string[100];//the length of the string to be entered from the keyboard needs to be properly adjusted to avoid the length of the input beyond the set range.
char c;
int I, num=0,sum=0,word=0; Defines whether word is used to indicate if a word is ending or if a new word begins;
printf ("Please input a line from the keyboard to query the English sentence, the number of words to count: \ n");
Gets (string); Gets the input string from the keyboard;
The number of English characters in the following statistical sentences;
for (i=0; (c=string[i])! = ' i=0 '; i++)//for Loop statement that iterates through each character in the sentence, initializes c!=, and executes i++ if the character ' n ', that is, does not reach the Terminator '/';
{
if ((' A ' <=string[i]&&string[i]<= ' Z ') | | (' A ' <=string[i]&&string[i]<= ' Z '))
sum++; The above is a conditional sentence, if the character within the range of A~z,a~z, then execute sum++, accumulate the number of English letters;
}
The number of English words in the following statistical sentences;
for (i=0; (c=string[i])! = ' i=0 '; i++)//for Loop statement that iterates through each character in the sentence, initializes c!=, and executes i++ if the character ' n ', that is, does not reach the Terminator '/';
{//' + ' is used as the terminator for a string. Its ASCII value is 0.
if (c< ' A ' | | C> ' Z ' &&c< ' A ' | | C> ' z ')//Set conditions: If character C encounters A~z and a~z outside the range of symbol characters, including encountering spaces ';
Word=0; When the above conditions are true, execute here, place word=0, indicating that no words have been encountered, or that a word has ended, and also means to start encountering the next new word;
else if (word==0)//when the condition (word==0) is true, executes the statement inside the curly braces below, and when word==0, indicates that no letter was encountered, that the word was not encountered, or that the last word had ended;
{
word=1; So word=1, that is, the next new word begins,
num++; Execute num++, accumulate the number of English words;
}
}
printf ("\ n");
printf ("The sentence you entered contains%d English characters and%d English words.) \ n ", sum,num);

}

Example: Input statement: Hello! My friend, how is it? (Note that there are spaces before and after the word)

C Language: Count the number of letters and words in the English sentence of the input line, with annotations!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.