Jump to content

How to see if array is equal to some word?

- - - - -

  • Please log in to reply
8 replies to this topic

#1
carbon

carbon

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts
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
}

#2
TheCompBoy

TheCompBoy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 272 posts
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/

#3
carbon

carbon

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts
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

#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
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.
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
carbon

carbon

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts

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
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
  • Location:Missouri
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.

#7
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas

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
carbon

carbon

    Learning Programmer

  • Members
  • PipPipPip
  • 37 posts

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
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
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