Counting the Vowels in a String | C Programming Example

  Рет қаралды 15,256

Portfolio Courses

Portfolio Courses

3 жыл бұрын

An example of how to count the values in a string with C. Source code: github.com/portfoliocourses/c.... Check out www.portfoliocourses.com to build a portfolio that will impress employers!

Пікірлер: 19
@MrMalchore
@MrMalchore 5 ай бұрын
When I read the title of this video, my first thought was to use the switch statement, as you did. But I went looking through the string.h header looking for existing string functions, and I found the strpbrk function that seemed perfectly designed for this scenario. To make a long story short, I wasn't able to get my "vowel counting" function to work. And it all came down to my while loop. Admittedly I like to use the Ternary operator a lot in my C code, so this while statement is doing an awful lot. Anyways... while ((str_ptr = strpbrk((str_ptr == NULL ? string : str_ptr), vowels)) != NULL) Where, str_ptr is a char pointer used to assign where in the string the next vowel was found. I'm making use of the fact an assignment is what is checked for NULL. And I can't figure out why this doesn't work. The str_ptr variable was declared liked this: char * str_ptr = NULL;
@rambhaktuchihaobito7987
@rambhaktuchihaobito7987 2 жыл бұрын
Thank you ❤️
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome! :-)
@hkafgh5185
@hkafgh5185 Жыл бұрын
Your amazing Thank you 🙂
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Thank you for the kind words, and you're very welcome! :-)
@ShadowlightSE
@ShadowlightSE Жыл бұрын
Usually I see people need to also send the length of the array to the function, since the only array size the function parameter has is a 8 byte int pointer, like fn(int *s, int arr_lenght). I do see this macro alot to get array length: sizeof(s) / sizeof(s[0]). That macro would not work in the function scope to get the length, i guess?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Great question! 🙂 And your guess is correct, that would not work to get the size of an array that's been passed to the function as a parameter. Because the function is really just getting a pointer (memory address) for the array. In the case of a string we don't need to provide the length of the string as an argument because the null terminator signifies the end of the string, or we can use strlen() from the string library. This video talks about passing arrays to functions: kzbin.info/www/bejne/pZaVk42Bn86KqMk. And this video covers pointers: kzbin.info/www/bejne/aHinmot9areZhKc. I'm just sharing them in case you might find them interesting. 🙂
@na50r24
@na50r24 Жыл бұрын
Is it possible to somehow measure the speed, resp. the efficiency of an algorithm? I switched from Python to C and solved this one with two loops: int my_vowel_count(char *s) { char vowels[] = {'I', 'A', 'E', 'O', 'U'}; int count = 0; for (int i = 0; i
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That's a great question! :-) And yes it's definitely possible to measure and compare the efficiency of algorithms. Usually when comparing algorithms we try to find their "Big O efficiency", which is a mathematical way of expressing how efficient an algorithm is with respect to the number of input values. It's a big topic that is beyond what I can describe in a comment, but if you're interested in comparing the efficiency of algorithms it's something that is worth learning more about: en.wikipedia.org/wiki/Big_O_notation. A simple way of just measuring "how fast" an algorithm implementation executes would be to use the time command on Unix-like operating systems like Linux and Mac: linuxize.com/post/linux-time-command/. For very small inputs, you may not be able to see a difference in time though. And in general this isn't always a great way of comparing algorithms... some code can run at different speeds depending on hardware things like the processor, memory, etc. The Big O notation is a way of comparing algorithms in a deeper way that's meaningful across different hardware, etc, because it looks at "how much work must be done for each unit of input". Hopefully this helps! :-)
@bkly_7772
@bkly_7772 Жыл бұрын
quick question how would this work with a bool? #include #include #include #include bool isVowel(std:: string user) { int vowelCheck = 0; for (int i = 0; i < user.length; i++) { switch (toupper(user[i])){ case 'A': case 'I': case 'E': case 'O': case 'U': vowelCheck++; } return vowelCheck; } }; int main() { std:: string userString; std:: cout userString; } Im having trouble getting the length in the for loop. If you could please explain I would Greatly Appreciate it,
@PortfolioCourses
@PortfolioCourses Жыл бұрын
If the function is checking if there is any vowel at all in the string it should be able to just return true if any case matches, and then return false at the end of the function which would only be reached if no case ever matches (i.e. if there are no vowels). :-)
@nub8312
@nub8312 Жыл бұрын
This video helped me so much, I tried another way without using libraries, is this still fine? int countVowels(char string[]) { int count = 0; for (int i = 0; string[i] != '\0'; i++) { switch (string[i]) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': count++; break; } } return count; }
@PortfolioCourses
@PortfolioCourses Жыл бұрын
That looks good to me. :-)
@nub8312
@nub8312 Жыл бұрын
Thanks a lot :D
@PortfolioCourses
@PortfolioCourses Жыл бұрын
You're welcome! :-)
@drig7847
@drig7847 Жыл бұрын
a shorter code using strpbrk() and strcpy() would look like this: #include #include int main() { char str[] = "Hello World"; int count = 0; while (1) { char *ptr = strpbrk(str, "aeiouAEIOU"); if (ptr != NULL) { count++; strcpy(str, ptr+1); } else { break; } } printf(" Vowel count: %d", count); return 0; }
@drig7847
@drig7847 Жыл бұрын
learned a lot from you, keep up the good work sir!
@PortfolioCourses
@PortfolioCourses Жыл бұрын
I'm glad you're enjoying the channel, thank you for including some of my videos in your playlist. :-)
@justcurious1940
@justcurious1940 6 ай бұрын
Nice , after reading comments I was able to stole some ideas --- > ALL rights reserved to the commentators below : // method 1 int vowels_count (char *string, int length){ int count = 0; for(int i = 0; i < length ; i++){ switch(tolower(string[i])){ case 'a' : case 'e' : case 'o' : case 'i' : case 'u' : count++; } } return count; } // method 2 int vowels_count2(char *string, int length){ int count = 0; char vowels[] = "aeiuo"; for(int i = 0 ; i < length ; i++){ for(int j = 0 ; vowels[j] != '\0' ; j++){ if(tolower(string[i]) == vowels[j]) ++count; } } return count; } // method 3 int vowels_count3(char *string){ int count = 0; char *ptr = string; // hello while(ptr){ ptr = strpbrk(ptr,"aeuioAEUIO"); if(ptr != NULL){ ++count; ++ptr; } } return count; }
哈莉奎因以为小丑不爱她了#joker #cosplay #Harriet Quinn
00:22
佐助与鸣人
Рет қаралды 9 МЛН
Son ❤️ #shorts by Leisi Show
00:41
Leisi Show
Рет қаралды 9 МЛН
Count The Vowels In A String | C++ Example
10:33
Portfolio Courses
Рет қаралды 2,2 М.
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
Check If A String Is A Palindrome | C Programming Example
10:56
Portfolio Courses
Рет қаралды 36 М.
how Google writes gorgeous C++
7:40
Low Level Learning
Рет қаралды 841 М.
How to split strings in C (strtok)
9:28
CodeVault
Рет қаралды 106 М.
Counting Occurrences Of A Word In A String | C Programming Example
14:56
Portfolio Courses
Рет қаралды 17 М.
Makefiles Make Your Life Easier
12:05
NeuralNine
Рет қаралды 91 М.
What's Your ENGLISH LEVEL? Take This Test!
21:31
Brian Wiles
Рет қаралды 1,8 МЛН
Думал, что нашел сокровище
0:34
Почему?
Рет қаралды 2,2 МЛН
😱 Защита цепи своими руками!
0:40
Тот самый Денчик
Рет қаралды 1,4 МЛН
Lehanga 🤣 #comedy #funny
0:31
Micky Makeover
Рет қаралды 18 МЛН
На МОРЕ с папой VS с мамой 🤪🌊🧜‍♀️ #comedy
0:25