How can I see in C if array is equal to "word"?
I think is this but Im not sure:
if(array1=="awesome"){
do this code
}
8 replies to this topic
#1
Posted 18 January 2012 - 07:43 AM
|
|
|
#2
Posted 18 January 2012 - 08:20 AM
Never worked with C but isn't it something like:
if(array1[PositionNumber]=="awesome"){
do this code
}
I think you use the square brackets just like in C++ and Java and the other languages.. As i said i never worked with C so please correct me if im wrong.
Think my post we're usefull? Please take your time and press the Like button at my post, Big Thanks!
For great C# & Android tutorials visit my blogg: http://www.thecompboy.com/
For great C# & Android tutorials visit my blogg: http://www.thecompboy.com/
#3
Posted 18 January 2012 - 08:28 AM
Its working. It was always confusing for me working with arrays... I thought if I input "awesome" it will take 7 places in an array. That is little confusing.
One more question. How to find a phrase in array.
Eg.
array=> la ha la la ha ha ha la la ha la
phrase=> la ha la
One more question. How to find a phrase in array.
Eg.
array=> la ha la la ha ha ha la la ha la
phrase=> la ha la
#4
Posted 18 January 2012 - 08:34 AM
In C, the == operator is not used for string equivalence. == tests for reference equivalence.
To test string equivalence in C, use strncmp().
strstr() is used to locate a substring within a larger string.
To test string equivalence in C, use strncmp().
strstr() is used to locate a substring within a larger string.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#5
Posted 18 January 2012 - 11:35 AM
gregwarner said:
strstr() is used to locate a substring within a larger string.
Can I do it without using any built-in function, only <stdio.h>? Is there any other way?
#6
Posted 18 January 2012 - 11:46 AM
Write your own implementation of strcmp then using pointers.
int mystrcmp(const char *s1, const char *s2)
{
while (*s1==*s2)
........
........
........
}
"The best optimizer is between your ears" - Michael Abrash
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.
Saying you can optimize a program is like saying you understand how a program works on every level of every facet on a specific machines configuration.
#7
Posted 18 January 2012 - 11:53 AM
carbon said:
Can I do it without using any built-in function, only <stdio.h>? Is there any other way?
You would have to loop over each character in the array until you found two differing characters, or a null terminator.
Any particular reason why you cannot use the C library functions? There are some pretty handy functions in string.h for doing this kind of stuff.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
#8
Posted 18 January 2012 - 11:55 AM
gregwarner said:
Any particular reason why you cannot use the C library functions? There are some pretty handy functions in string.h for doing this kind of stuff.
Its competition.
#9
Posted 18 January 2012 - 12:04 PM
I see. Have fun! Good luck!
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top










