Skip to content

Instantly share code, notes, and snippets.

@mkroman
Last active December 28, 2015 09:39
Show Gist options
  • Save mkroman/7480493 to your computer and use it in GitHub Desktop.
Save mkroman/7480493 to your computer and use it in GitHub Desktop.
#include <string>
#include <cstring>
#include <iostream>
char BAND_COLOR_CODE[10][8] =
{
"black", "brown", "red", "orange", "yellow",
"green", "blue", "violet", "grey", "white" };
int find_cstring_in_array(char array[][8], size_t array_size, const char* needle)
{
std::cout << array[0] << std::endl;
for (unsigned int i = 0; i < array_size; i++)
{
if (strcmp(array[i], needle) == 0)
return i;
}
return -1;
}
int main(int argc, char* argv[])
{
const char* needle = "brown";
size_t array_size = sizeof(BAND_COLOR_CODE) / sizeof(*BAND_COLOR_CODE);
int result = find_cstring_in_array(BAND_COLOR_CODE, array_size, needle);
if (result > 0)
std::cout << "Success! (" << BAND_COLOR_CODE[result] << ")" << std::endl;
else
std::cout << "No success :<" << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment