Counting the Vowels in a String | C Programming Example

  Рет қаралды 17,496

Portfolio Courses

Portfolio Courses

Күн бұрын

Пікірлер: 19
@MrMalchore
@MrMalchore 9 ай бұрын
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;
@ShadowlightSE
@ShadowlightSE 2 жыл бұрын
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 2 жыл бұрын
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). :-)
@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. :-)
@rambhaktuchihaobito7987
@rambhaktuchihaobito7987 2 жыл бұрын
Thank you ❤️
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re welcome! :-)
@hkafgh5185
@hkafgh5185 2 жыл бұрын
Your amazing Thank you 🙂
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you for the kind words, and you're very welcome! :-)
@justcurious1940
@justcurious1940 10 ай бұрын
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; }
@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! :-)
Counting Occurrences Of A Word In A String | C Programming Example
14:56
Portfolio Courses
Рет қаралды 18 М.
How to Fight a Gross Man 😡
00:19
Alan Chikin Chow
Рет қаралды 20 МЛН
I was just passing by
00:10
Artem Ivashin
Рет қаралды 18 МЛН
C# program - count vowels and consonants in a string
4:19
String In Char Array VS. Pointer To String Literal | C Programming Tutorial
9:58
15 Years Writing C++ - Advice for new programmers
4:04
SyncMain
Рет қаралды 1,3 МЛН
Count The Words In A String | C Programming Example
8:08
Portfolio Courses
Рет қаралды 13 М.
51 - FINDING FREQUENCY OF VOWELS, CONSONANTS,DIGITS,SPACES,WORDS - C PROGRAMMING
20:28
you will never ask about pointers again after watching this video
8:03
Count the Number of Vowels in a String | Java
8:37
B Tech Computer Science
Рет қаралды 22 М.
how Google writes gorgeous C++
7:40
Low Level
Рет қаралды 957 М.
31 nooby C++ habits you need to ditch
16:18
mCoding
Рет қаралды 830 М.
How to Fight a Gross Man 😡
00:19
Alan Chikin Chow
Рет қаралды 20 МЛН