• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How to check an array for a particular value?

mOeeOm

Platinum Member
How do you scan and array to find a specific value and then print out how many times it occured? I tried writing the code down below, I know it will be way off, but any help is appreciated.

Thanks. when i run the program, nothing happens.
 
> check an array for a particular value

Since it's a character array do you mean a character or a sub-string like "the" ?

character: == (case sensitive) or change case before == for case-insensitive
substring: strncmp (case sensitive) or build a case-insensitive version like Visual C++ _strnicmp.
 
Originally posted by: DaveSimmons
> check an array for a particular value

Since it's a character array do you mean a character or a sub-string like "the" ?

character: == (case sensitive) or change case before == for case-insensitive
substring: strncmp (case sensitive) or build a case-insensitive version like Visual C++ _strnicmp.

well in the sentence, I want it to tell me how many times the word ''the'' occured in the array.
 
then you want to compare one string to part of another string.

Does your homework say anything about case sensitive? That is, for
"The quick brown fox jumped over the lazy dog"

should "the" match both "the" and "The" ?

strncmp() will let you do a case-sensitive comparison ( the != The).

One trick for comparing case-INsensitve ( the == The == THE ) is to make copies of both string, converting both copies to lowercase.

if ( >= 'A' && <= 'Z' )
x = x - 'A' + 'a'

 
Originally posted by: DaveSimmons
then you want to compare one string to part of another string.

Does your homework say anything about case sensitive? That is, for
"The quick brown fox jumped over the lazy dog"

should "the" match both "the" and "The" ?

strncmp() will let you do a case-sensitive comparison ( the != The).

One trick for comparing case-INsensitve ( the == The == THE ) is to make copies of both string, converting both copies to lowercase.

if ( >= 'A' && <= 'Z' )
x = x - 'A' + 'a'

well its not homework im just practicing it for a coming up test, a friend told me to look for practice problems online and this was one of them, it just says write an array to print out how many times the word ''the'' appears in the sentence ''the cow jumps over the moon'', so thats two times.
 
You really have to learn the basics. I think you may have been getting too much help on these forums that has let you use our code without having to figure out things for yourself.

In your code above, you declare an int called i. An int is an integer value, remember?
then you declare a character called "the"

Do you know what a character is? A character is a single character on the keyboard, for example 't' or '5' or ','. Note that each of those is just ONE symbol. You have to use an array of characters to hold more than just a single value. if you want to use "the" you need a character array that can hold 4 characters.

Now, you never actually assign a value to "the", but I'm pretty sure you are acting like you want it to contain the string "the".

Thenyou do this:
for(;i = the; ++i);

You try to assign "the" to i.

You never gave "the" a value. Even if you had, it would have been a character, and you're trying to assign it to an int.

How about you do something for us. Take the code you wrote above, and post it again in another post. Then try to explain to us, very explicity, what it is that each line of your code does. If you can tell us what it does, that means that you actually know what you're doing. If you can't tell us what it does, that means your going to have to go look it up before you finish the post, and it might help you learn the material.

I'm not trying to be a smartass here, I'm just trying to encourage you to learn exactly wha tit is that your code is doing.
 
With respect to parsing the code looking for a character string match; Also, take into acocunt finding embedded strings.

Do you wish to count "the" as a match when it is inside the word their.
 
^ good advice from notfred (and good point Doug 🙂 ).

If you've gotten this far by just copying code instead of writing it and understanding what you've written, you're probably going to bomb the tests. Even if you manage to pass this course you won't have learned the material and won't be prepared for future classes or writing any code in the real world.
 
Originally posted by: notfred
You really have to learn the basics. I think you may have been getting too much help on these forums that has let you use our code without having to figure out things for yourself.

In your code above, you declare an int called i. An int is an integer value, remember?
then you declare a character called "the"

Do you know what a character is? A character is a single character on the keyboard, for example 't' or '5' or ','. Note that each of those is just ONE symbol. You have to use an array of characters to hold more than just a single value. if you want to use "the" you need a character array that can hold 4 characters.

Now, you never actually assign a value to "the", but I'm pretty sure you are acting like you want it to contain the string "the".

Thenyou do this:
for(;i = the; ++i);

You try to assign "the" to i.

You never gave "the" a value. Even if you had, it would have been a character, and you're trying to assign it to an int.

How about you do something for us. Take the code you wrote above, and post it again in another post. Then try to explain to us, very explicity, what it is that each line of your code does. If you can tell us what it does, that means that you actually know what you're doing. If you can't tell us what it does, that means your going to have to go look it up before you finish the post, and it might help you learn the material.

I'm not trying to be a smartass here, I'm just trying to encourage you to learn exactly wha tit is that your code is doing.

Ahh I see, I was trying to make it so everytime i = it would do the for loop and add 1 to the total, so 0 + 1, then 1+1 = 2, but ya, I see where I went wrong. but the other way you say is to make a character for the array.

so...

char word1[] = ''the''
char word2[] = ''the cow jumped over the moon''

how would you write it to find how many times the first array appears in the second one? sorry guys if its been frustrating, but this is a one time course I have to take, after I pass it I will never have to take it again because I will not be going into electrical engineering, which does this heavily, so I'm just trying to get by. I have a practical test soon about it so I want to have learned the stuff so I can....just regurgitate the information and make it by. Sorry if it sounds lazy...🙁
 
This kind of "practice" is worthless if you're just going to get other people to write the answers.

You might memorize the answer to this specifc problem, but then on the test you'll be asked to count letters instead of strings, or to find one string then stop and print, or . . .

You might as well give up now on studying, and hope that the gibberish you write on the test looks enough like real code that you can pass the course.
 
Back
Top