How to find a string in an array of strings in C
Вставка
- Опубліковано 10 січ 2022
- Source code can be found here:
code-vault.net/lesson/qwc2wv1...
===== Support us through our store =====
code-vault.net/shop
===== Check out our website =====
code-vault.net
===== Check out our Discord server =====
discord.code-vault.net
Really appreciate this video.
Your way of teaching is so powerful
Hi, Thank you for this amazing video.
Can you please create complete C language tutorials playlist from beginner to advance in a practical hands on way. It would be good for us to get some hands on experience on the industry based problems.
I'll look into it. Thanks for the recommendation!
very good
You can also use strcasecmp does the same as strcmp but ignores the case.
Oh cool! I didn''t even know about the strcasecmp function. Thanks!
I'm still on the fence about covering non-standard functions on the channel though (same reason why I didn't use strlwr in this video)
What do you think? Should I start covering all these non-standard yet popular functions?
@@CodeVault Yes Please
Thank you so much for this video.
I have a question tho, at 6:27
can't we just use else{} instead whar you wrote in line 20?
Else for the if in the for loop you mean?
@@CodeVault yes , instead of writing that i>=3 does it work with just an else ?
Not really, since the message would be printed for every element that is not equal to the one you're searching
can you repeat please the need for `input[strlen(input)-1]=0` line?
since there is already a null character, why overwrite last input char with 2nd one?
A new line character: '
' is read as the last input char when the return key is pressed, In this instance it is not wanted and is therefore removed by replacing it with a null terminator.
Take a look at the video I pointed to: code-vault.net/lesson/yxllgxvfmb:1603733521595
It explains this and why we use fgets instead of scanf
@@CodeVault I use this always for removing end of line , works for windows and linux input[strcspn(input, "
")] = 0;
Would a "do...while" loop with a flag variable not be cleaner than a full-fledged for loop with a break instruction?
A bit maybe. You'd still need to have the definition of i, increment and the condition somewhere in that loop.
int i = 0;
do {
// ...
} while (i < 3 && strcmp(arr[i], input) != 0);
I could be biased but reading a for loop with a break is much easier to understand than a do...while loop that looks like this
You can use whichever you feel is right for you though!
@@CodeVault I was more thinking about something like :
int i = 0, notfound = 0;
do {
notfound = strcmp(arr[i], input);
i++;
} while (i < 3 && notfound);
But as you said, except for memory or execution time constraints, only the dev's taste differs :)
Can u explain about the oriented objects in c++
Yes! I plan on making a large series about OOP
@@CodeVault oooh that would be awsome
Sir video is helpful but I want to search( 4 ft × 15 inch ,0.9 21) as a whole string to be found can you help me please ( I want to use data structure too but it says _parameter error
The current program finds only till
4 ft only 4 digits i want whole string to be searched at once with data structure please see to it and send source code to improve it
There are two options: You can either manually iterate over the string and check if the string is what you're searching for or, use a regex library (if you need to find any number in the string)
@@CodeVault
Sir, thanks for info
But please share source code
Array a={(4 ft x 15 inch, 0.93,21),(4ft x 15 inch ,0.93,21),(4ft x 12 inch),0.93,21)}
Output: (4 ft x 15 inch, 0.93,21) =>2
(4ft x 12 inch),0.93,21) =>1
and this is how we know you favourite fruit or colour is Orange.. 😛😜
Thanks, but I want function of it
You could copy everything in a function and set the array of strings as parameter and the result as a return