0

I'm trying to assign a specific value of char array (or string) through conditionals depending on the value of char.

Suppose c is a char declared before the code with a specific value of some sort.

  char fruit[];
  if (c == 'R') {
    fruit[] = "Red";
  }
  else if (c == 'Y') {
    fruit[] = "Yellow";
  }
  else if (c == 'G') {
    fruit[] = "Green";
  }
  else if (c == "B") {
    fruit[] = "Blue";
  }

This code is obviously wrong but should give an idea on what I'm trying to do.

I am either planning to use a correct version of this for a simple program or have to go through around four times more conditionals to manually print the said string values which would be an immense pain.

3
  • 1
    char fruit[]; and fruit[] = "Red"; is not valid. You need to take char *fruit and then fruit = "Red"; but this is string literal not modifiable. OR you can use char fruit[100] then strcpy(fruit,"Red"). Commented Sep 18, 2014 at 6:05
  • 2
    You can't assign to an array, but you can copy to it. However, then it needs to have a size of the largest data you want to copy to it (and for strings, don't forget the terminator). Commented Sep 18, 2014 at 6:05
  • else if (c == "B") is incorrect,also use strcpy for copying strings. Commented Sep 18, 2014 at 6:07

3 Answers 3

4
  char *color;   // for modern compilers, should be  const char *color
  switch (c)
  {
  case 'R':    color = "Red";       break;
  case 'Y':    color = "Yellow";    break;
  case 'G':    color = "Green";     break;
  case 'B':    color = "Blue";      break;
  default:     color = "<unknown>"; break;
  }

I took exception at illogical variable names so renamed fruit to color, but this approach is one way to achieve what I think you are asking.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry. Totally missed it.
2

The best way to do this would be to use a switch statement, but you'll first need to tell the compiler how big you want fruit[] to be, if you're not going to allocate it dynamically based on the character you detect.

Unless you're going to be dealing with fruits that have colors with really long names, I'd say 16 characters is enough for a demonstration. Hence:

#include <stdio.h>
#include <string.h>

void fruity_printer(char c) {
    char fruit[16] = { 0 }; // initialize your arrays!

Now, a simple switch statement on char c

    switch (c) {
         case 'R':
            strcpy(fruit, "Red");
            break;
         // add more cases as needed

         default: // what happens if we don't have a case for it?
            strcpy(fruit, "Rainbow"); 
            break;
    }

    printf("The fruitabulous color is: %s\n", fruit);
    return;
    }

Note that I've used strcpy() here, because I'm sure that I know that I'll be writing within bounds of the destination. You'd never just arbitrarily copy something where the length wasn't known at compile time like that, you'd use strncpy() instead, which takes another argument for length.

Things you'll also want to do is convert c in the fruity printer to either upper or lower, so you don't have to deal with both cases.

Another way would be to allocate your memory dynamically, but the semantics of figuring out what it should be are still best served by just using a simple switch.

Comments

0

You can use switch case to that:

#include<stdio.h>
#include<string.h>

int main (int argc, char *argv[])
{
      char c;
      char fruit[100];
      printf("Enter the character: ");
      scanf(" %c",&c);
      switch(c)
      {
          case 'R' : strcpy(fruit,"Red"); break; 
          case 'Y' : strcpy(fruit,"Yellow"); break; 
          case 'G' : strcpy(fruit,"Green"); break; 
          case 'B' : strcpy(fruit,"Blue"); break; 
          default : puts("No color");
      }
      printf("%s\n",fruit);
 }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.