用C++语言查找一个字符串单词的个数
C语言 统计字符个数(指针)

时间限制:
1000毫秒
内存限制:
32768 K字节
总提次数:
689次
成功提交次数:
455次
判题规则:
严格比较
问题描述
输入一行字符,统计并输出其中数字字符、英文字母和其它字符的个数。
要求:
1、编写一函数void count(char *s,int *pdigit,int *pletter,int *pother),其中s为输入的字符串,*pdigit, *pletter,*pother分别表示字符串中数字、字母和其他字符的个数。
int dight=0;
int letter=0;
int other=0;
gets(s);
count(s,&dight,&letter,&other);
printf("%d %d %d \n",dight,letter,other);
return 0;
}
输入
输入1个字符串。
输出
输出数字、字母和其他字符的个数。每个数后有一空格。
输入样列
wenzhou university 1933
输出样例
4 17 2
出处
ymc
答案:
#include <stdio.h>
#include <string.h>
void count(char s[],int *pdight,int *pletter,int *pother)
{
int n;
int i;
n=strlen(s);
for(i=0;i<n;i++)
在一个字符串中,统计大写字母个数,小写字母个数,其他字符个数的四种算法

在⼀个字符串中,统计⼤写字母个数,⼩写字母个数,其他字符个数的四种算法题⽬描述:编写程序,输出字符串中的⼤写字母、⼩写⼩母和其他的个数。
如有⼀个字符串"Helle, This is A test textfile.123456, tannkyou!!",则其⼤写字母个数:3,⼩写字母个数:29,其他字符个数:18.这⾥提供了四种算法,第⼀种是我们⽐较好理解的,也属于硬编码问题,其他三种⽅法要借助JAVA语⾔的jdk提供的api。
⽅法⼀:<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>js分析字符串内容</title><!--实现⼀个函数,输出某字符串⾥有⼏个⼤写字母,⼩写字母,数字,其他符号。
字符串由形参指定 --><script>var str = prompt("请随意输⼊⼤写字母⼩写字母数字及符号等");function analyze(aa){var a = 0;var A = 0;var n = 0;var other = 0;for (var i=0;i<aa.length;i++){var c = aa.substr(i,1); //aa.charAt(i);if (c>='a' && c<='z'){a++;}else if(c>='A' && c<='Z'){A++;}else if(c>='0' && c<='9'){n++;}else{other++;}}document.write("⼩写字母为"+a,"⼤写字母为"+A,"数字为"+n,"其他字符为"+other);}</script></head><body onload="analyze(str)"></body></html>[java]1. //⽅法⼀:在利⽤每个字符的Unicode码在a~z之间,调⽤jdk提2. //供的String类的charAt取出字符串每⼀个字符,逐个进⾏⽐较来判定3.4. class FindLetter {5. public static void main(String[] args) {6. String str = "Helle, This is A test textfile.123456, tannk you!!";7. int upCount = 0;8. int lowCount = 0;9. int otherCount = 0;10.11. for(int i = 0; i < str.length(); i++) {12. char c = str.charAt(i);13. if(c >= 'a' && c <= 'z') {14. lowCount++;15. } else if(c >= 'A' && c <= 'Z') {16. upCount++;17. } else {18. otherCount++;19. }⽅法⼆:[java]1. //⽅法⼆:⽤jdk的Character类的isUpperCase⽅法和isLowerCase⽅法2.3. class FindLetter1 {4. public static void main(String[] args) {5. String str = "Helle, This is A test textfile.123456, tannk you!!";6. int upCount = 0;7. int lowCount = 0;8. int otherCount = 0;9.10. for(int i = 0; i < str.length(); i++) {11. char c = str.charAt(i);12. if(Character.isUpperCase(c)) {13. upCount++;14. } else if(Character.isLowerCase(c)) {15. lowCount++;16. } else {17. otherCount++;18. }19. }20. System.out.println("⼤写字母个数:" + upCount);21. System.out.println("⼩写字母个数:" + lowCount);22. System.out.println("其他字母个数:" + otherCount);23. }24. }⽅法三:[java]1. //⽅法三:先定义两个字符串a到z和A到Z,再逐个取出str字符串中的每个字母,2. //⽤indexOf()⽅法来判断字符是否在这这个定义的字符串中,在⼤写字母这⼀⾏,3. //⼤写字母的计数器就加1,在⼩写字母这⾏,⼩写字母就加⼀,否则其他字母计算器4. //加15.6. class FindLetter2 {7. public static void main(String[] args) {8. String low = "abcdefghijklmnopqrstuvwxyz";9. String up = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";10. int lowCount = 0;11. int upCount = 0;12. int otherCount = 0;13. String str = "Helle, This is A test textfile.123456, tannk you!!";14.15. for(int i = 0; i < str.length(); i++) {16. char c = str.charAt(i);17. if(low.indexOf(c) != -1) {18. lowCount++;19. } else if(up.indexOf(c) != -1) {20. upCount++;21. } else {22. otherCount++;23. }⽅法四:[java]1. //把str分别转化为⼤写和⼩写⼤写⽤sU ⼩写 sL2. //然后通过与原串⽐较来统计个数3.4. class FindLetter3 {5. public static void main(String[] args) {6. String str = "Helle, This is A test textfile.123456, tannk you!!";7. String sU = str.toUpperCase();8. String sL = str.toLowerCase();9. int lowCount = 0;10. int upCount = 0;11. int otherCount = 0;12. for(int i = 0; i < str.length(); i++) {13. char charSTR = str.charAt(i);14. char charSU = sU.charAt(i);15. char charSL = sL.charAt(i);16.17. //如果不是字母,是其他字符,则直接⽤otherCount来计数18. if(Character.isLetter(charSTR)) {19. //如果原串与转换过后的⼤写字母串相等,则原来字符为⼤写字母,20. //若与⼩写字母相等,则为⼩写字母21. if( charSTR == charSU) {22. upCount++;23. } else if(charSTR == charSL) {24. lowCount++;25. }26. } else {27. otherCount++;28. }29. }30.31. System.out.println("⼤写字母个数:" + upCount);32. System.out.println("⼩写字母个数:" + lowCount);33. System.out.println("其他字母个数:" + otherCount);34. }35. }这四种算法都有正确的输出:⼤写字母个数:3⼩写字母个数:29其他字母个数:18。
接收输入的一行字符,统计出字符串中包含数字的个数.

统计字符串中数字个数的方法有很多种,下面我们分别介绍几种常见的方法。
方法一:遍历统计1. 我们可以通过遍历字符串的每一个字符,判断字符是否是数字,并统计数字的个数。
实现代码如下:```pythondef count_digits(s):count = 0for char in s:if char.isdigit():count += 1return countinput_str = input("请输入字符串:")result = count_digits(input_str)print("字符串中包含数字的个数为:", result)```方法二:使用正则表达式2. 正则表达式是一种可以用来匹配字符串的强大工具,我们可以利用正则表达式来匹配数字,并统计匹配到的数字个数。
实现代码如下:```pythonimport redef count_digits(s):pattern = repile(r'\d')result = pattern.findall(s)return len(result)input_str = input("请输入字符串:")result = count_digits(input_str)print("字符串中包含数字的个数为:", result)```方法三:使用内置函数3. Python内置的字符串方法也提供了一些方便的函数来处理字符串。
我们可以使用`isdigit()`函数来判断字符是否是数字,然后统计数字的个数。
实现代码如下:```pythondef count_digits(s):count = sum(1 for char in s if char.isdigit())return countinput_str = input("请输入字符串:")result = count_digits(input_str)print("字符串中包含数字的个数为:", result)```以上就是三种统计字符串中数字个数的方法,你可以根据实际情况选择适合自己的方法来使用。
C++程序测量一个字符串中的单词个数(多种方法)

#include<stdio.h>void main(){char str[81];int i,num=0,word=0;char c;cout>>"please input the string:\n";gets(str);for(i=0;(c=str[i])!='\0';i++){if(c==' ')word=0;else if(word==0){word=1;num++;}}cout>>"There are %d words in the line.\n">>num;}这个练习题不错,如果把空格换成非字符符号,可以用来统计文章中的单词数。
#include<stdio.h>#include<stdlib.h>int main(){char str[50];int word;int n=0;int i;printf("Input:");gets(str);for(i=0;i<50-1;i++){if(str[i]==' '){word=1;}if(word){n++;word=0;}}printf("Output: There are is %d in teh line.\n",n);system("pause");return 0;}给你个思路吧。
设定一个字符数组,或者直接用string对象,从键盘接收一个字符串到该字符数组或字符串对象中。
然后设一个变量i用以遍历字符串,如果遇到第i位是空格或者标点,则空格或标点数加1,并检查第i-1位是否为字母,如果i-1位是字母,说明刚刚遍历过去的是一个单词,则单词数加1,否则(i-1位不是字母)就继续往下走。
当然你还要设三个变量存储空格、标点和单词的数量。
c语言统计一个字符串中单词的个数

c语言统计一个字符串中单词的个数这个程序可以自动清除多余的空格。
#include "stdio.h"int count_word(char *str);void main(){char str1[80];int sum=0;puts("\n please enter a string");gets(str1);sum=count_word(str1);printf("there are %d words in this sentence",sum);}int count_word(char *str){int count,flag;char *p;count=0;flag=0;p=str;while(*p!='\0')/*当字符串没有到结尾的时候,重头到尾的循环*/{if(*p==' ')/*假如字符串遇到空格,就将flag清零,同时可以过滤掉多余的空格*/ flag=0;else if(flag==0)/*当字符串不是空格时,假如flag为0,那么计算器加1,既是遇到空格后的第一个字符时*/{flag=1;/*将flag标记回1,这样在遇到第一个字符后的字符时可以将他过滤掉,直到遇到空格时,在清零*/count++;}p++;}return count;}C语言编程题求教----输入一个字符统计其中的单词个数输入一串字符统计其中的单词个数。
*/各个单词间用空格隔开空格数可以是多个/* 例子:Input:Let's go to room 209count=5这是我做的程序:#include <stdio.h>int main(void){char ch;int c,m;printf("Input words:");c=1;while((ch=getchar())!='\n'){if(ch==' ')c++;}printf("count=%d\n",c);return 0;}但是如果是多个空格就要多统计出单词个数来。
编写一个程序,接受用户输入的一段英文文字,统计出其中的字符个数、单词个数和句子的个数。(设。。。

编写⼀个程序,接受⽤户输⼊的⼀段英⽂⽂字,统计出其中的字符个数、单词个数和句⼦的个数。
(设。
这个题⽬⽤到正则表达式,正则表达式是⼀种可以⽤于模式匹配和替换的规范.字符串对象(String)调⽤matches()可以判断当前字符串对象是否与参数regex指定的正则表达式匹配.String上可以使⽤正则表达式的操作,实际上是利⽤了java.util.regex.Pattern与java.util.regex.Matcher的功能.当调⽤String的matches()⽅法时,实际上是调⽤了Pattern的静态⽅法matches(),这个⽅法会返回boolean值,表⽰字符串是否符合正则表达式.将正则表达式视为⼀个对象来重复使⽤,使⽤pattern的静态⽅法compile()进⾏编译.这个⽅法会返回⼀个Pattern的实例,这个实例代表正则表达式,之后可以重复使⽤Pattern实例的matcher()⽅法来返回⼀个Matcher的实例,代表符合正则表达式的实例.1import java.util.Scanner;2import java.util.regex.Matcher;3import java.util.regex.Pattern;45public class Test5_1 {6public static void main(String[] args){7int countC=0,countW=0,countS=0; //分别表⽰字符,单词和句⼦的个数8 Scanner in=new Scanner(System.in);9 System.out.print("input a english sentence:");10 String st=in.nextLine(); //⽤nextInt接收,是以回车作为分隔符.⽽⽤next接受,是以空格作为分隔符.11 Pattern p2=pile("[ ,.!?]"); //与[]中的任意⼀个字符匹配即可.分别有空格,逗号,句号,感叹号,问号.⽤以统计单词个数.12 Matcher m2=p2.matcher(st); 13 Pattern p1=pile("[a-zA-Z ,.!?]"); //匹配⼀段话中字符的个数14 Matcher m1=p1.matcher(st);15 Pattern p3=pile("[.!?]"); //匹配⼀段话中句⼦的个数16 Matcher m3=p3.matcher(st);17boolean result2=m2.find(); 18while(result2){19 countW++;20 result2=m2.find(); //判断是否找到并统计个数21 }22boolean result1=m1.find();23while(result1){24 countC++;25 result1=m1.find();26 }27boolean result3=m3.find();28while(result3){29 countS++;30 result3=m3.find();31 }32 System.out.println("单词个数:"+countW);33 System.out.println("字符个数:"+countC);34 System.out.println("句⼦个数:"+countS);35 }36 }。
(完整版)C语言统计文件中的字符数、单词数以及总行数

C语言统计文件中的字符数、单词数以及总行数统计文件的字符数、单词数以及总行数,包括:每行的字符数和单词数文件的总字符数、总单词数以及总行数注意:空白字符(空格和tab缩进)不计入字符总数;单词以空格为分隔;不考虑一个单词在两行的情况;限制每行的字符数不能超过1000。
代码如下#include <stdio.h>#include <string.h>int *getCharNum(char *filename, int *totalNum);int main(){char filename[30];// totalNum[0]: 总行数totalNum[1]: 总字符数totalNum[2]: 总单词数int totalNum[3] = {0, 0, 0};printf("Input file name: ");scanf("%s", filename);if(getCharNum(filename, totalNum)){printf("Total: %d lines, %d words, %d chars\n", totalNum[0], totalNum[2], totalNum[1]);}else{printf("Error!\n");}return 0;}/*** 统计文件的字符数、单词数、行数** @param filename 文件名* @param totalNum 文件统计数据** @return 成功返回统计数据,否则返回NULL**/int *getCharNum(char *filename, int *totalNum){FILE *fp; // 指向文件的指针char buffer[1003]; //缓冲区,存储读取到的每行的内容int bufferLen; // 缓冲区中实际存储的内容的长度int i; // 当前读到缓冲区的第i个字符char c; // 读取到的字符int isLastBlank = 0; // 上个字符是否是空格int charNum = 0; // 当前行的字符数int wordNum = 0; // 当前行的单词数if( (fp=fopen(filename, "rb")) == NULL ){perror(filename);return NULL;}printf("line words chars\n");// 每次读取一行数据,保存到buffer,每行最多只能有1000个字符while(fgets(buffer, 1003, fp) != NULL){bufferLen = strlen(buffer);// 遍历缓冲区的内容for(i=0; i<bufferLen; i++){c = buffer[i];if( c==' ' || c=='\t'){ // 遇到空格!isLastBlank && wordNum++; // 如果上个字符不是空格,那么单词数加1isLastBlank = 1;}else if(c!='\n'&&c!='\r'){ // 忽略换行符charNum++; // 如果既不是换行符也不是空格,字符数加1isLastBlank = 0;}}!isLastBlank && wordNum++; // 如果最后一个字符不是空格,那么单词数加1isLastBlank = 1; // 每次换行重置为1// 一行结束,计算总字符数、总单词数、总行数totalNum[0]++; // 总行数totalNum[1] += charNum; // 总字符数totalNum[2] += wordNum; // 总单词数printf("%-7d%-7d%d\n", totalNum[0], wordNum, charNum);// 置零,重新统计下一行charNum = 0;wordNum = 0;}return totalNum;}在D盘下创建文件demo.txt,并输入如下的内容:运行程序,输出结果为:上面的程序,每次从文件中读取一行,放到缓冲区buffer,然后遍历缓冲区,统计当前行的字符和单词数。
C语言获取字符串中最长的单词

charword(charstr[])
{inti,k,flag=0,max=0;
charstr1[10],ch[10];
for(i=0;str[i]!='\0';i++)
if((65<=str[i]&&str[i]<=90)||(97<=str[i]&&str[i]<=122))\\如果该字符是字母
}
}
printf("输入的字符串中,最长的单词有%d个字母,该单词是%s\n",max,str1);
}
main()
{chara[100];
printf("请输入一行字符:\n");
gets(a);
word(a);\\调用word函数
}
函数大致流程图如下(“<=”是赋值):
在C-FREE5.0上的运行结果:
{if(flag==0)\\如果该字符是字母,且新单词未开始
{
k=0;
ch[k]=str[i];
flag=1;}
else if(i==strlen(str)-1)\\如果该字符是字母,新单词已开始,且已到达字符串结尾
{k++;
ch[k]=str[i];
ch[k+1]='\0';
if(k+1>max)\\k+1表示单词的长度
{max=k+1;
strcpy(str1,ch);
}
}
else\\如果该字符是字母,新单词已开始,且未到达字符串结尾
{k++;