Loop-06. count the number of words in a line of text,-06 a line
1/** 2 * Main. c 3 * loop-06. count the number of words in a line of text 4 * Created on: July 25, 2014 5 * Author: boomkeeper 6 ******************* 7 */8 9 # include <stdio. h> 10 11 int main () {12 char input, B = ''; 13 int count = 0; 14 15 while (input = getchar ())! = '\ N') {16 // printf ("% c \ n", input); 17 if (input! = ''& B ='') 18 count ++; 19 B = input; 20} 21 22 printf ("% I \ n", count ); 23 24 return 0; 25}
Reference Source:
Http://tieba.baidu.com/p/3078959002
Question link:
Http://pat.zju.edu.cn/contests/basic-programming/%E5%BE%AA%E7%8E%AF-06
C language design for counting the number of words in a text file
# Include <stdio. h>
# Include <ctype. h>
Void main ()
{
Char ch;
Int numberofword = 0, wordStart = 0;
FILE * fp1 = fopen ("test.txt", "r ");
FILE * fp2 = fopen ("result.txt", "w ");
If (fp1 = NULL | fp2 = NULL)
{
Puts ("cannot open file! ");
Return;
}
While (! Foef (fp1 ))
{
Ch = fgetc (fp1 );
If (isalpha (ch) & wordStart = 0)
{
WordStart = 1;
}
Else if (! Isalpha (ch) & wordStart = 1)
{
Numberofword ++;
WordStart = 0;
}
}
Fprintf (fp2, "% d", numberofword );
Fclose (fp1 );
Fclose (fp2 );
}
Program to count the number of words in a single string,
The idea is to traverse all the characters. Once non-letter characters are found, the number of words plus 1 can be found ~~~
Below is the C # code.
----------------------------------------------------------
Using System;
Using System. Collections. Generic;
Namespace lianxi
{
Class MainClass
{
Public static void Main (string [] args)
{
String str1;
Str1 = "My name is cuihao, what about you? ";
Console. WriteLine (word (str1 ));
Console. Read ();
}
Public static int word (string strg)
{
Char [] str1 = strg. ToCharArray ();
Int I, j = 0;
For (I = 0; I <str1.Length; I ++)
{
If (!
(
(Str1 [I]> = 65 & str1 [I] <= 90) |
(Str1 [I]> = 97 & str1 [I] <= 122)
)
)
{
J ++;
}
}
Return j;
}
}
}