NickName:莊雅惠 Ask DateTime:2013-06-15T16:34:49 Are there way shorter than this?(Counting number of occurrences of a char in a string in C) Copyright Notice:Content Author:「莊雅惠」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.Link to original article:https://stackoverflow.com/questions/17121702/are-there-way-shorter-than-thiscounting-number-of-occurrences-of-a-char-in-a-s Answers Daniel Goldberg 2013-06-15T08:42:47 If you mean by characters, probably. If you mean by runtime? Then you're far from the most optimal solution.\n\nRecursion is vastly slower than looping, and if compiled precisely as written, will use up large quantities of stack space for large enough strings. ", nullptr 2013-06-15T08:49:11 Well, if the function must be named frequence, then the iterative way is probably a bit shorter in characters (after removing unnecessary spaces of course):\n\nint k=0; while (*s) k += c == *s++; return k;\n", Products recommended by Amazon More >>> SanDisk 128GB microSDXC-Card, Licensed for Nintendo-Switch - SDSQXAO-128G-GNCZN HUANUO Dual Monitor Stand - Adjustable Spring Monitor Desk Mount Swivel Vesa Bracket with C Clamp, Grommet Mounting Base for 13 to 27 Inch Computer Screens - Each Arm Holds 4.4 to 14.3lbs AMD Ryzen 5 5600X 6-core, 12-Thread Unlocked Desktop Processor with Wraith Stealth Cooler Timberland Men's 6-Inch Premium Waterproof Boot More about “Are there way shorter than this?(Counting number of occurrences of a char in a string in C)” related questions Counting the number of occurrences of a string within a string What's the best way of counting all the occurrences of a substring inside a string? Example: counting the occurrences of Foo inside FooBarFooBarFoo Show Detail Counting number of occurrences of a char in a string in C I have the string str char *str = "100.10b.100.100"; I want to count the occurrences of '.' in str, preferably a one-liner. (If possible no loops) My approach would be the standard strchr: in... Show Detail Counting Occurrences in char array Write a function that accepts a character array as input, counts the number of occurrences for each character in the array, and outputs the result in format(eg {'c', 'e', 'e', 'e', 'a', 'q'} woul Show Detail Java Exception in Counting number of occurrences of character in a string I am a java beginner and want to do some logic stuffs. I have started with a simple program (i.e) to find the total number of occurrences of a character in a string. But i ended up with this except... Show Detail Are there way shorter than this?(Counting number of occurrences of a char in a string in C) int frequence(char *s,char c) { return *s ? (*s==c)+frequence(s+1,c) : 0; } Are there way shorter than this? Show Detail Mutex - counting occurrences of a char in files using threads Hi I am writing a code for counting the number of occurrences of a certain letter inside one or more files using thread. I have to use one thread for each file e use a mutex to modify the global to... Show Detail Counting the number of occurrences of a set of different substrings within a string in PostgreSQL I have a database with lots of textual information, and I would like to count the number of times certain terms occur in a given text field. I know how to do this in Python, but as I am planning on Show Detail Find number of string separators in a string I am using the following to find the number of separators in a string: char * string = "xi--len--xia" char * split_on = "--"; size_t len_joined_string = strlen(string); size_t len_split_on = strlen( Show Detail Count number of occurrences for each char in a string I want to count the number of occurrences of each character in a given string using JavaScript. For example: var str = "I want to count the number of occurrences of each char in this string&qu... Show Detail Fastest way to count the number of occurrences of a string I was wondering what is the fastest way to count the number of occurrences of a string (needle) within another string (haystack). The way I'm doing it is: int findWord(char * file, char * word){ ... Show Detail