声明一个较大的char型数组,待输入完成后依次检查数组元素,除第一个单词可能需要个别处理外,其余以空格后跟字母e69da5e6ba90e799bee5baa6e997aee7ad9431333363376438或其它字符后跟字母的就认为是一个单词,累加计数即可。代码如下:int main(int argc,char *argv[]){ char text[300];//定义一个空间较大的char数组存放输入的text int i,w; printf("Input a text...\n"); fgets(text,300,stdin);//用fgets既可接收空格又能限制长度 w=text[0]>='a' && text[0]<='z' || text[0]>='A' && text[0]<='Z';//开头是字母则w置1 for(i=1;text[i];i++) if((text[i]>='a' && text[i]<='z' || text[i]>='A' && text[i]<='Z') && !(text[i-1]>='a' && text[i-1]<='z' || text[i-1]>='A' && text[i-1]<='Z')) w++;//非字母后跟着字母则为一个单词 printf("A total of %d word(s).\n",w); return 0;}运行样例如下图:

#include <stdio.h>main(){ char string[81]; int i, num=0,word=0; char c; gets(string); for(i=0;(c=string[i])!='\0';i++) //确定没百有到结束符度 { if(c==' ') //当知c获得的字符为空格的道时候专,单引号内属为空格 word=0; else if(word==0) { word=1; num++; } } printf("there are %d words in the line.\n",num);} 本回答被网友采纳

int CountWord(LPCSTR strFilter){ int nRet = 0; int nLen = 0; LPCSTR pCurrentHead = NULL; pCurrentHead = strFilter; //计算长度 nLen = strlen(strFilter); if(nLen == 0) { nRet =0; goto ERROR_OUT; } else nRet = 1; while(1) { //找到空格 pCurrentHead = strchr(pCurrentHead,' ');if(NULL == pCurrentHead) break; else { nRet +=1; pCurrentHead = pCurrentHead+1; } }ERROR_OUT: return nRet;}int main(){ char szTest[512]; sprintf(szTest,"warning C4508: 'main' : function should return a value; 'void' return type assumed."); int nCount = CountWord(szTest); printf("String:<%s>'s word count = %d\n",szTest,nCount); return;}这个是比较简单的实现,没有考虑乱输一堆标点符号的问题,如果要做好点,就检查出每个e68a847a6431333264626536被空格分开的子串,然后对每个子串用正则表达式匹配,剔出非英文单词的子串

include<stdio.h>int main(){char string[81];int i,num=0,word=0;char c;gets(string);for(i=0;(c=string[i])!='\0';i++)if(c=='')word=0;else if(word==0){word=1;num++;printf("there are%d words in this line:\n",num);return 0;}

没用过,不过你可以尝试一下统计空格符号和逗号,句号,当然,句子得很标准才行,一个单词后面跟一个空格或者逗号句号

#include <stdio.h>main(){ char string[81]; int i, num=0,word=0; char c; gets(string); for(i=0;(c=string[i])!='\0';i++) //确定没有百到结束符 { if(c==' ') //当c获得的度字符为空回格的时答候,单引号内为空格 word=0; else if(word==0) { word=1; num++; } } printf("there are %d words in the line.\n",num);}

#include&lt;stdio.h&gt;#include&lt;string.h&gt;#include&lt;stdlib.h&gt;main(){char string[100];//根据拟从键盘输入的字串的长度需要适当调整,要避免输入的长度超出设定的范围。char c;int i,num=0,sum=0,word=0;//定义word用来指示一个单词是不是结束或新单词是否开始;printf("请从键盘输入一行需要查询的英文句子,进行单词数量统计e69da5e6ba90e799bee5baa631333431353362:\n\n");gets(string);//从键盘获得输入的字符串;//以下统计句子中的英文字符个数;for(i=0;(c=string&lt;i&gt;)!='\0';i++)//for循环语句,遍历句子中的每个字符;初始化i=0;若字符c!='\0',即未到达结束符'\0'的话,执行i++;{if(('A'&lt;=string&lt;i&gt;&&string&lt;i&gt;&lt;='Z')||('a'&lt;=string&lt;i&gt;&&string&lt;i&gt;&lt;='z'))sum++;//以上为条件句,如果字符在A~Z,a~z范围之内的话,则执行sum++,累加英文字母个数;}//以下统计句子中的英文单词个数;for(i=0;(c=string&lt;i&gt;)!='\0';i++)//for循环语句,遍历句子中的每个字符;初始化i=0;若字符c!='\0',即未到达结束符'\0'的话,执行i++;{//'\0'用作字符串的结束符。它的ASCII数值是0。if(c&lt;'A'||c&gt;'Z'&&c&lt;'a'||c&gt;'z')//设定条件:如果字符c遇到A~Z和a~z范围之外其它符号字符的话,包括遇到空格'';word=0;//上面条件为真时,执行这里,置word=0,表示未遇到单词,或一个单词已结束,同时也意味着要开始遇到下一个新单词;else if(word==0)//当条件(word==0)为真,执行下面花括号里面的语句;当word==0时,表示未遇到字母,即未遇到单词,或上一个单词已结束;{word=1;//那么置word=1,即,表示下一个新单词开始,num++;//执行num++,累加英文单词的个数;}}printf("\n");printf("您输入的这句英文句子中共包含%d个英文字符,%d个英文单词。\n",sum,num);}示例:输入语句:Hello!My friend,how are you?(注意单词的前后有空格)扩展资料:include用法:#include命令预处理命令的一种,预处理命令可以将别的源代码内容插入到所指定的位置;可以标识出只有在特定条件下才会被编译的某一段程序代码;可以定义类似标识符功能的宏,在编译时,预处理器会用别的文本取代该宏。插入头文件的内容#include命令告诉预处理器将指定头文件的内容插入到预处理器命令的相应位置。有两种方式可以指定插入头文件:1、#include&lt;文件名&gt;2、#include"文件名"如果需要包含标准库头文件或者实现版本所提供的头文件,应该使用第一种格式。如下例所示:#include&lt;math.h&gt;//一些数学函数的原型,以及相关的类型和宏如果需要包含针对程序所开发的源文件,则应该使用第二种格式。采用#include命令所插入的文件,通常文件扩展名是.h,文件包括函。

if(str[i]==' '||str[i]=='\0')这里 你是考虑到copy 结尾\0也会加百一但实际上 当str[i] == '\0'时 循环因为for(i=0;str[i]!='\0';i++)已经退出了所以 这部度分不会加一 导致出错可以改知成这样。for(i=0;;i++){if(str[i]==' '||str[i]=='\0')count++;if(str[i]=='\0') break;}先判断道 累加 再退出 本回答被提问者和网友采纳

#include<stdio.h>int main(){ zhidaochar str[256]; int i,count=0; printf("Please input an English sentence:\n"); gets(str); for(i=0;str[i]!='\0';i++) { 专if(str[i]==' ') count++; //你这只是算有几属个空格字符数 } count+=1; //单词数=空格数+1 printf("The number of words are:%d\n",count); return 0;  //main()函数是有返回值的}

试编写代码如下:#include<stdio.h>#include<string.h>#include<ctype.h>struct word{     char str[30];  //单词     int num;       //单词出现的次数}words[500];int sum;void chuli(char s[]){      int i,j;      int flag=0;     for(i=0;i<=sum;i++)      {          if(strcmpi(words[i].str,s)==0)          {                       words[i].num++;              flag=1;             }         }      if(flag==0)      {          strcpy(words[sum].str,s);       words[sum].num++;          sum++;      }       }void paixu1(){       int i,j;       struct word a;     for(i=0;i<sum;i++)     {        for(j=i+1;j<sum;j++)              if(strcmp(words[i].str,words[j].str) > 0)           {                a=words[j];                     words[j]=words[i];           words[i]=a;         }  }}void paixu2(){        int i,j;        struct word a;      for(i=0; i<sum; ++i) {        for(j=sum-1; j>i; --j) {         if(words[j].num>words[j-1].num)             {                 a=words[j];                      words[j]=words[j-1];            words[j-1]=a;          } } } }int main(){     char s[30];     int imax,istart,iend,i,j,flag=0;      char str[] = "The president told the audit authorities to keep their thoughts and actions in line with the CPC Central Committee, resolutely safeguard the centralized and unified leadership of the committee and implement the requirements of the committee.";      char delims[] = " ";     char *sword = NULL;   sum=0;   for(i=0;i<500;i++)      words[i].num=0;   sword = strtok(str, delims);     while( sword != NULL ) {        sscanf(sword,"%[a-zA-Z]",s);        strlwr(s);        chuli(s);     sword = strtok( NULL, delims );     }      printf("该文章共有:%d个单词\n",sum);    paixu1();    printf("\n按单词字典顺序7a686964616fe78988e69d8331333365653264升序排列:\n");    for(i=0;i<sum;i++)     printf("%s, %d\n",words[i].str,words[i].num);          paixu2();    printf("\n按出现次数降序排列:\n");    for(i=0;i<sum;i++)     printf("%s, %d\n",words[i].str,words[i].num);    return 0;}实际运行截图:

'把下e799bee5baa6e997aee7ad94e4b893e5b19e31333365653264面代码复制到窗体,添加1个list 和一个command按钮运行就可以了,和一个文本框,数据输入在文本框里Private Type Englishword As Stringcount As Long End Type'判断是不是英文 Private Function En(ByVal s As String) As BooleanIf s = "" Then En = False: Exit Functions = LCase(s)For i = 1 To Len(s)a = Asc(Mid(s, i, 1))If a < 97 Or a > 122 Then En = False: Exit FunctionNextEn = True End Function'统计数组内各个英文出现次数 Private Function TongJi(ByVal T_txt As String) As English()s = Split(T_txt, " ")n = 0Dim English() As EnglishReDim English(0)For i = 0 To UBound(s)If s(i) <> "" And En(s(i)) = True Thentemp = FalseFor j = 0 To UBound(English)If English(j).word = "" ThenEnglish(j).word = s(i): English(j).count = 1: Exit ForElseIf English(j).word = s(i) Thentemp = True: English(j).count = English(j).count + 1: Exit ForEnd IfNextIf temp = False ThenReDim Preserve English(n): English(n).word = s(i): English(n).count = 1: n = n + 1End IfEnd IfNextTongJi = English End FunctionPrivate Sub Command1_Click()Dim e() As Englishe = TongJi(Text1)ClsFor i = 0 To UBound(e)If e(i).count > 0 Then List1.AddItem e(i).word & " 出现过: " & e(i).count & " 次"Next End Sub

#include <iostream>#include <map>#include <algorithm>#include <string>#include <iterator>#include <vector>#define N 50using namespace std;int cmp(const pair<string, int>& x, const pair<string, int>& y) //vector里实现单词出现次数排序{      return x.second > y.second;  }     void sortMapByValue(map<string, int>& tMap,vector<pair<string, int> >& tVector)//将map元素转存为vector  {      for (map<string, int>::iterator curr = tMap.begin(); curr != tMap.end(); curr++)           tVector.push_back(make_pair(curr->first, curr->second));           sort(tVector.begin(), tVector.end(), cmp); //对vector对序,实现按单词出现次数排序}  int main(){    char ch;    string a;    string la;    map <string,int > v;    map <string,int>::iterator it;    while(getline(cin,la)){//按行读入,ctrl+z结束循环        int i=0;        //if(la=="000")break;//调试用        while(i!=la.size()){//对读入的字符行逐字符比对            ch=la[i];            if(ch>='A' && ch<='Z')//大写字母转换成小写,再构成单词                a+=char(ch-'A'+'a');            else if(ch>='a' && ch<='z')//小写字符照抄到单词中                a+=ch;            else {//遇到空格或者标点符号                if(a.size()>0){//如果前面构成的单词非空                //cout<<a<<endl;//调试用                    it=v.find(a);//map容器还没有此单词                    if(it==v.end())                        v.insert(pair<string,int>(a,1));//插入map容器中                    else                        v[a]++;//map容器中已有以此单词为键的项,该键计数器value值加1                    a.clear();//已加入容器,构成单词a清空,准备下一次构造                }            }            ++i;     }    }    cin.clear();//清除输入流,不然,结束上面大while循环时输入的ctrl+z    //会影响到整个程序e799bee5baa6e79fa5e98193e59b9ee7ad9431333365653263,导致程序结束退出,无法再进行下面输入操作    cout<<"1:按出现次数降序排列, 2:按单词字典顺序升序排列"<<endl;    int k;    cin>>k;//选择输出方式    if(k==2){//map中默认已自动按key(即单词)排序,直接输出即符合字典顺序    for(it=v.begin();it!=v.end();++it)        cout<<(*it).first<<","<<(*it).second<<endl;    }    if(k==1){//对map中的value排序:因map特性,不支持sort()算法直接排序        //所以,通过将map内容转存到vector容器中进行排序    vector <pair<string,int > > tVector;      sortMapByValue(v,tVector);      for(int i=0;i<tVector.size();i++)          cout<<tVector[i].first<<","<<tVector[i].second<<endl;      }        return 0;} 追问 大哥,我要的是c,不是c++。。。 追答 要纯C语言版,私聊。 本回答被网友采纳

#include main() { char string[81]; int i, num=0,word=0; char c; gets(string); for(i=0;(c=string[i])!='\0';i++) //确定没有到结束知符 { if(c==' ') //当c获得的道字符为空格回的时候,答单引号内为空格 word=0; else if(word==0) { word=1; num++; } } printf("there are %d words in the line.\n",num); } 追问 可以清晰一点吗

public static void main(String[] args){System.out.println("请输入英语片段,以';'结束:");Scanner scanner = new Scanner(System.in);String str = "";int dc = 0;int zc = 0;int jc = 0;while(scanner.hasNext()){str = scanner.next();zc += str.length();dc++;System.out.println(str);if (str.contains(".")){jc += 1;}if (str.contains(";")){break;}}// scanner.System.out.println("单词个数为:" + dc);System.out.println("字母数为:7a686964616fe4b893e5b19e31333366306433" + (zc - 1));System.out.println("句数为:" + jc);}扩展资料:import java.io.*;public class test{ public static void main(String[] args) throws IOException{ System.out.print("请输入一串字母:");BufferedReader br=new BufferedReader(new InputStreamReader(System.in));String str=br.readLine();int char1=0,letter=0,sentence=0;for(int i=0;i<str.length();i++){char ch = str.charAt(i);if(ch!=' '){ char1++;}if(ch==' '||ch=='/n'){ //有空格,加一单词if(i!=0&&str.charAt(i-1)!=' ')letter++;}if(ch=='?'||ch=='!'||ch=='.'){           sentence++; }}System.out.println("字符数:"+char1);System.out.println("单词个数:"+letter);System.out.println("句子数:"+sentence);}}import java.io.*;public class test{ public static void main(String[] args) throws IOException{ System.out.print("请输入一串字母:");BufferedReader br=new BufferedReader(new InputStreamReader(System.in));String str=br.readLine();int char1=0,letter=0,sentence=0;for(int i=0;i<str.length();i++){char ch = str.charAt(i);if(ch!=' '){ char1++;}if(ch==' '||ch=='/n'){ //有空格,加一单词if(i!=0&&str.charAt(i-1)!=' ')letter++;}if(ch=='?'||ch=='!'||ch=='.'){           sentence++; }}System.out.println("字符数:"+char1);System.out.println("单词个数:"+letter);System.out.println("句子数:"+sentence);}}

public static void main(String[] args) { e68a84e8a2ade799bee5baa631333264656666 System.out.println("请输入英语片段,以';'结束:"); Scanner scanner = new Scanner(System.in); String str = ""; int dc = 0; int zc = 0; int jc = 0; while(scanner.hasNext()) { str = scanner.next(); zc += str.length(); dc++; System.out.println(str); if (str.contains(".")) { jc += 1; } if (str.contains(";")) { break; } } // scanner. System.out.println("单词个数为:" + dc); System.out.println("字母数为:" + (zc - 1)); System.out.println("句数为:" + jc); } 追问 大师, while(scanner.hasNext()) 怎么理解,没学过,求解释 追答 读取屏幕所输入的单词,以空格为组!如how are you? scanner.hasNext()) 就是3次 本回答被提问者采纳

package com.sh.lw.regxp;import java.util.Scanner;import java.util.regex.Matcher;import java.util.regex.Pattern;public class RegXp { /** * 计算单词总数 * @param s */ public int countWord(String s) { 636f70797a686964616f31333264656666s = s.trim(); Pattern pattern = null; String str = "\\s"; pattern = Pattern.compile(str); Matcher m = pattern.matcher(s); int i = 0; while (m.find()) { i++; } return i + 1; } /** * 计算句子总数 * @param s * @return */ public int countStatement(String s) { s = s.trim(); Pattern pattern = null; String str = "[.!?]{1}"; pattern = Pattern.compile(str); Matcher m = pattern.matcher(s); int i = 0; while (m.find()) { i++; } return i; } /** * 计算字符总数 * @param s * @return */ public int countChar(String s) { s = s.trim(); Pattern pattern = null; String str = "\\S"; pattern = Pattern.compile(str); Matcher m = pattern.matcher(s); int i = 0; while (m.find()) { i++; } return i; } /** * 入口函数 * @param args */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); RegXp rx = new RegXp(); int _char, _stmt, _word; _char = rx.countChar(s); _stmt = rx.countStatement(s); _word = rx.countWord(s); System.out.println("字符总数:" + _char); System.out.println("句子总数:" + _stmt); System.out.println("单词总数:" + _word); }}

public static void main(String []args) { 知Scanner s1=new Scanner(System.in); System.out.println("请输入一段道英文文字:"); String words=s1.nextLine(); CheckWords(words); } public static void CheckWords(String words) { int juzi=0,danci=0; for(int i=0;i<words.length();i++) { if(words.charAt(i)=='.' ||专 words.charAt(i)=='!' || words.charAt(i)=='?') { juzi++; } if(words.charAt(i)==' ') { danci++; } } System.out.println("句子"+juzi+"个属,单词"+danci+"个."); } 追问 CheckWords(words);显示错误,怎么回事?