Newsgroup: it.comp.lang.c
Titolo: URGENT help required on searching within a character array

Hi,
I am trying to create a function which has a constant string with a word and
a number just before the word.  I need it to search for a matching word
within the string, go to the character just before the start of the word
(i.e. the number) and return that value.  I have been trying may ways, using
strstr and for statements and just cannot get anything to work.  Here is
what I am at now.. :
void convert()
{
 char colour[6];
 int colourLength,colourValue;
 bool found;
 char colours[60] =
"0Black1Brown2Red3Orange4Yellow5Green6Blue7Violet8Grey9White";
 printf("Type in the color you wish to know the number of: ");
 scanf("%S",colour);
 colourLength = strlen(colour);
 char tempcolour[6] = colour;
 found = false;
 for (int x = 0; x <60 ; x++)
  {
  for (int y = 0; y <=colourLength; y++)
  {
   tempcolour[y] = colours[x + y]
  }
  if(tempcolour != colour) continue;
  else {found = true; break}
 }
 if(found) {printf("the number is %c", colours[x-1]);
}
If anyone knows how to make this work, or can show me another way it could
be done please let me know it would be much appreciated, and also could u
email it to 
mi...@gois.net
 thanks in advance!
-- 
comp.lang.c.moderated - moderation address: 
cl...@plethora.net

Here's your homework:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char findcolour(const char *colour)
{
    char *where;
    static const char colours[] =
        "0Black1Brown2Red3Orange4Yellow5Green6Blue7Violet8Grey9White";
    if (!(where = strstr(colours, colour)))
        return 0;
    return *(where - 1);
}
int main(void)
{
    const char *test[] =
        { "Black", "Brown", "Red", "Orange", "Yellow", "Green", "Blue",
        "Violet", "Grey", "White"
    };
    unsigned i, which, ntsts = sizeof test / sizeof *test;
    char digit;
    for (i = 0; i < 10; i++) {
        which = ntsts * (rand() / (1. + RAND_MAX));
        digit = findcolour(test[which]);
        if (digit)
            printf("Digit %c found for colour \"%s\"\n", digit,
                   test[which]);
        else
            printf("No match for colour \"%s\"\n", test[which]);
    }
    return 0;
}
Digit 1 found for colour "Brown"
Digit 0 found for colour "Black"
Digit 3 found for colour "Orange"
Digit 1 found for colour "Brown"
Digit 0 found for colour "Black"
Digit 8 found for colour "Grey"
Digit 7 found for colour "Violet"
Digit 5 found for colour "Green"
Digit 5 found for colour "Green"
Digit 2 found for colour "Red"
-- 
Carius est nobis flagellari p doctrina quam nescire.
[leofre ys us beon beswungen for lare thaenne hit ne cunnan.]
 - MS Cotton Tiberius A, xv, fol. 60v (British Library)


Hi,
this function may works for you:
/**
 * colorToNum
 *
 * Description: 
 *   Retrieves the number of a named color specified in a String of the
 *   format:
 *     {<NUMBER><COLORNAME>}+
 *     e.g.: 0Black1Blue2Red
 * Parameter:
 *   colors - String which contains the specified colors.
 *   searchColor - Name of color to retrieve the number of.
 * Returns:
 *   Number of color if found (>= 0) or -1 if not found.
 */
int colorToNum(const char* colors,
	       const char* searchColor) {
  int first = 0, last = 0; /* First and last character of a Token like a color
                          name or a number value. */
  int i = 0, j = 0, k = 0; /* Counter variables for loops which step through
                              Strings while Tokenizing the input. */
  int number = -1; /* Stores the number of the color. */
  int inString = 0; /* Flag which designates if reading a String. */
  /* Step through the whole colors tokenize them and search for the
     specified color */
  for (i = 0; i <= strlen(colors); i++) {    
    if ((inString == 0) && (isdigit(colors[i]) == 0)) {
      /* first character found */
      inString = 1;
      first = i;
    }
    if ((inString == 1) && 
	((isdigit(colors[i]) != 0) || (i >= strlen(colors)))) {
      /* At the end of the color name */
      inString = 0;
      last = i;
      if (last > first) {
	/* Store name of the color */
	char tmpStr[last - first + 1];
	for (j = 0; j < last - first; j++)
	  tmpStr[j] = colors[first + j];
	tmpStr[j] = '\0';  /* Terminate Token with ASCII-Z */
	/* Check if color was found */
	if (strcmp(searchColor, tmpStr) == 0) {
	  /* Color was found, retrieve number */
	  last = --first;
	  while ((first > 0) && (isdigit(colors[first]) != 0))
	    first--;
	  if (first > 0)
	    first++;  /* readjust index - its on the last char of the
			 prior color or at 0 */
	  /* Convert String to number */
	  number = 0;
	  for (k = first; k <= last; k++) {
	    number = (number * 10) + atoi(&colors[k]);
	  }
	  return number; /* well done */
	}
      }
    }
  }
  return -1; /* color not defined */
}
Greetings,
  Markus


...
Hi Michael,
try this !!!!
#include <stdio.h>
#include <string.h>
void main()
{
  char  allColors[10][7] ={"Black","Brown","Red","Orange","Yellow",
                           "Green","Blue","Violet","Grey","White"};
  char inputColor[10];
  int i;
  printf("Type in the color you wish to know the number of: ");
  scanf("%s",inputColor);
  for (i=0; i<10;i++)
  {
    if (!strcmp(inputColor, allColors[i]))
	  break;
  }
  if (i < 10)
    printf("\nthe number is %d", i); 
  else
    printf("\ncolor not found"); 
  return;
}
best regards
Alexander


email it to 
mi...@gois.net
 thanks in advance!
<<Remove the del for email>>
