C语言 计算C中字符串中字符的出现次数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4235519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Counting number of occurrences of a char in a string in C
提问by Mike
I have the string str
我有字符串 str
char *str = "100.10b.100.100";
I want to count the occurrences of '.'in str, preferably a one-liner. (If possible no loops)
我想计算'.'in的出现次数str,最好是单行的。(如果可能没有循环)
My approach would be the standard strchr:
我的方法是标准strchr:
int i = 0;
char *pch=strchr(str,'.');
while (pch!=NULL) {
i++;
pch=strchr(pch+1,'.');
}
回答by R.. GitHub STOP HELPING ICE
Here's the way I'd do it (minimal number of variables needed):
这是我的做法(需要最少数量的变量):
for (i=0; s[i]; s[i]=='.' ? i++ : *s++);
回答by tvanfosson
Look, ma, no loops.
看,妈妈,没有循环。
int c = countChars( s, '.' );
int countChars( char* s, char c )
{
return *s == 'size_t CountChars(const char *s, char c)
{
size_t nCount=0;
if (s[0])
{
nCount += ( s[0]==c);
if (s[1])
{
nCount += ( s[1]==c);
if (s[2])
{
nCount += ( s[2]==c);
if (s[3])
{
nCount += ( s[3]==c);
if (s[4])
{
nCount += ( s[4]==c);
if (s[5])
{
nCount += ( s[5]==c);
if (s[6])
{
nCount += ( s[6]==c);
if (s[7])
{
nCount += ( s[7]==c);
if (s[8])
{
nCount += ( s[8]==c);
if (s[9])
{
nCount += ( s[9]==c);
if (s[10])
{
/* too long */
assert(0);
}
}
}
}
}
}
}
}
}
}
}
return nCount;
}
'
? 0
: countChars( s + 1, c ) + (*s == c);
}
But, I'd actually use a loop, since that's the correct control structure to use.
但是,我实际上会使用循环,因为这是要使用的正确控制结构。
回答by Michael J
OK, a non-loop implementation (and yes, it is meant as a joke).
好的,一个非循环实现(是的,这是一个笑话)。
int i, count;
for (i=0, count=0; str[i]; i++)
count += (str[i] == '.');
回答by Fabian Giesen
Without loops is going to be hard since there's no standard C library function that does this and you need to look at all chars :)
没有循环会很困难,因为没有标准的 C 库函数可以执行此操作,您需要查看所有字符 :)
I'll take the obvious solution:
我将采取明显的解决方案:
int count_characters(const char *str, char character)
{
const char *p = str;
int count = 0;
do {
if (*p == character)
count++;
} while (*(p++));
return count;
}
Feel free to squeeze the two lines of actual code into one if you have to :)
如果必须,请随意将两行实际代码合二为一:)
回答by cdhowie
I'd still throw this in a function, parametrizing the source string and the character to search for.
我仍然会将它扔到一个函数中,参数化源字符串和要搜索的字符。
size_t count = 0;
while(*str) if (*str++ == '.') ++count;
回答by Steve Jessop
If you're keen on a one-liner (well, two-):
如果您热衷于单线(好吧,两线):
int countChar(char *s, char letter) {
return ((*s) ? (((*s++ == letter)? 1:0)) + countChar (s, letter)): 0);
}
回答by Sanjeev
//I guess it should work. One line and no loop.
//我想它应该可以工作。一行,无循环。
for(i = 0; str[i] != 'for(c = 0; *str != ' int i=0,count=0;
char *str = "100.10b.100.100";
a:
if(str[i]=='.')
count++;
i++;
if(str[i])
goto a;
'; str++)
{
if(*str == '.')
++c;
}
'; i++)
{
if(str[i] == '.')
++c;
}
回答by WENDYN
If you really want a one-liner:
如果你真的想要一个单线:
keeping strintact (in case you want to use it later)
for(i = 0, c = 0; str[i] != '\0'; (str[i] == '.')? c++: 0, i++);if you don't care about strvariable
for(c = 0; *str != '\0'; (*str == '.')? c++: 0, str++);- additionally you can subtract previous strlen to make it usable again
保持str完整(以防您以后想使用它)
for(i = 0, c = 0; str[i] != '\0'; (str[i] == '.')? c++: 0, i++);如果你不关心str变量
for(c = 0; *str != '\0'; (*str == '.')? c++: 0, str++);- 此外,您可以减去之前的 strlen 以使其再次可用
but as you can see, it's pretty ugly so I would advise doing something like this:
但正如你所看到的,它很丑,所以我建议做这样的事情:
1
2
3
4 #include <ctype.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9
10 size_t bytewise_pop_count(
11 unsigned char * bp, size_t l
12 ) {
13 if ( (bp) && (l) ) {
14 return bytewise_pop_count(bp+1, l-1) + (bp[0] ? 1 : 0);
15 }
16 return 0;
17 }
18
19 void mercilessly_complement_bytes(
20 unsigned char * bp, size_t l
21 ) {
22 /*
23 transform
24 0 -> 1
25 !0 -> 0
26 */
27 if ( (bp) && (l) ) {
28 bp[0] = bp[0] ? 0 : 1;
29 mercilessly_complement_bytes(bp+1, l-1);
30 }
31 }
32
33 void xor_bytes(
34 unsigned char * bp1, unsigned char * bp2, size_t l
35 ) {
36 /* stores result in bp2 */
37 if ( (bp1) && (bp2) && (l) ) {
38 bp2[0] ^= bp1[0];
39 xor_bytes(bp1+1, bp2+1, l-1);
40 }
41 }
42
43
44 int main(int argc, char * * argv) {
45 char c;
46 size_t count;
47 size_t l;
48 char * string;
49 char * t;
50
51 if (argc < 3) {
52 fprintf(stderr,
53 "\n"
54 "==> not enough arguments -- need char and string\n"
55 "\n"
56 );
57 return EXIT_FAILURE;
58 }
59
60 c = argv[1][0];
61 string = argv[2];
62
63 if ( l = strlen(string) ) {
64 t = malloc(l);
65 memset(t, c, l);
66 xor_bytes(string, t, l);
67 mercilessly_complement_bytes(t, l);
68 count = bytewise_pop_count(t, l);
69 free(t);
70 } else {
71 count = 0;
72 }
73
74 if ( isprint(c) ) {
75 printf(
76 "\n"
77 "==> occurences of char ``%c'' in string ``%s'': %zu\n"
78 "\n"
79 , c, string ? string : "<NULL>", count
80 );
81 } else {
82 printf(
83 "\n"
84 "==> occurences of char ``%hhu'' in string ``%s'': %zu\n"
85 "\n"
86 , c, string ? string : "<NULL>", count
87 );
88 }
89 return EXIT_SUCCESS;
90 }
or
或者
##代码##回答by fuddin
I dont like goto,still,
我不喜欢转到,仍然,
##代码##回答by x y z
everytime you run this code baby dijkstra cries :)
每次运行此代码时,婴儿迪杰斯特拉都会哭:)
##代码##
