c 统计字符串中字符出现的个数

1、单纯用数组来解题

思路:从左往右循环,每次碰到一个字符就和左边的字符串比较,如果有相同的就右移,如果没有找到相同的就从这个位置向右统计个数并输出。

 1 #include<stdio.h>
 2
 3 void calCount(char arr[])
 4 {
 5         int i,j,count,had;
 6         i = j = count = had = 0;
 7         while(arr[i] != ‘\0‘)
 8         {
 9                 count = 0;
10                 had = 0;
11                 for(j=0; j<i; j++)
12                 {
13                         if(arr[j] == arr[i])
14                         {
15                                 had = 1;
16                                 break;
17                         }
18                 }
19                 if(had == 1)
20                 {
21                         i++;
22                         continue;
23                 }
24                 else
25                 {
26                         count++;
27                         j = i+1;
28                         while(arr[j] != ‘\0‘)
29                         {
30                                 if(arr[j] == arr[i])
31                                         count++;
32                                         j++;
33                         }
34                         printf("%c%d,",arr[i],count);
35                 }
36                 i++;
37         }
38 }
39
40 int main(void)
41 {
42         char arr[50];
43         scanf("%[^.]", arr);//接收任何字符,碰到‘.‘就结束,
44         printf("%s\n", arr);
45         calCount(arr);
46         return 0 ;
47 }

2、用ASCII码来计算

 1 #include<stdio.h>
 2
 3    void getChCount(char *p)
 4    {
 5        char arr[127] = {0};
 6        int i;
 7        while(*p != ‘\0‘)
 8        {
 9            arr[*p]++;
10           p++;
11       }
12       for(i=0; i<127; i++)
13       {
14           if(arr[i] > 0)
15               printf("%c%d\n",i,arr[i]);
16       }
17   }
18
19   int main(void)
20   {
21       char brr[100];
22       scanf("%[^.]",brr);//遇到字符.停止接收字符
23       getChCount(brr);
24       return 0;
25   }
时间: 2024-07-30 17:08:01

c 统计字符串中字符出现的个数的相关文章

java统计字符串中字符及子字符串个数

import java.util.Scanner;public class Counter { static Scanner scanner = new Scanner(System.in); public static void count(String s) { int low, upper, num, others; low = upper = num = others = 0; for (int i = 0; i < s.length(); i++) { if (Character.is

技巧之C#统计字符串中字符出现的次数(转)

方法1.自定义类 class CharNum { private char c; private int num; public char C { get { return c; } } public int Num { get { return num; } set { num = value; } } public CharNum(char ch) { this.c = ch; this.num = 1; } } static void Main(string[] args) { /* */

找错误——下面的程序意图在于统计字符串中字符数1的个数,可惜有瑕疵

#include<stdio.h>#define maxn 10000000+10int main(){ char s[maxn]; scanf("%s",s); int tot=0; for(int i=0;i<strlen(s);i++)   if (s[i]==1)tot++; printf("%d\n",tot);} 改程序至少有3个问题,一个导致程序无法运行,另一个导致结果不正确,还有一个导致效率低下.你能找到并改正他们吗? 关于此题我只

【c++程序】统计字符串中字符出现次数

#include<iostream> #include<string> //#include<cstring> using namespace std; int main() { string str; cout<<"input some text:"<<endl; getline(cin,str); //char str[200]; //cin.getline(str,200); int cnt[256]={}; for(i

统计字符串中字符出现的次数

var a = {}; var str = 'gouod'.split(""); str.forEach(function (v, i) { a[v] = a[v] == undefined ? 1 : a[v] + 1; }) console.info(a)

统计字符串中字符出现的次数(||和&amp;&amp;的区别)

var str = "ProsperLee"; // || 返回第一个为真的表达式的值,若全为假则返回最后一个表达式的值 // && 返回第一个为假的表达式的值,若全为真则返回最后一个表达式的值 String.prototype.charCount = function(){ var json = {}; for (var i = 0, l = this.length; i < l; i++) { json[this[i]] = json[this[i]] + 1

转载:js实现统计字符串中特定字符出现个数的方法

//js统计字符串中包含的特定字符个数 function getPlaceholderCount(strSource) {   //统计字符串中包含{}或{xxXX}的个数   var thisCount = 0;   strSource.replace(/\{[xX]+\}|\{\}/g, function (m, i) {     //m为找到的{xx}元素.i为索引     thisCount++;   });   return thisCount; }

统计字符串中每个字符的个数

1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title>统计字符串中每个字符的个数@</title> 6 </head> 7 <body> 8 </body> 9 10 <script type="text/javascript"&

统计字符串中,各个字符的个数(回炉练习)

__author__ = 'ZHHT' #!/usr/bin/env python # -*- coding:utf-8 -*- #统计字符串中,各个字符的个数 #比如:"hello world" 字符串统计的结果为: h:1 e:1 l:3 o:2 d:1 r:1 w:1 a = "hello world" b = set(a) for i in b: if i == ' ': c = a.count(i) i = '空格' print("%s出现%d次