简体   繁体   English

根据C中的char值分配char数组

[英]Assigning char array based on the value of char in C

I'm trying to assign a specific value of char array (or string) through conditionals depending on the value of char. 我试图通过取决于char的值的条件来分配char数组(或字符串)的特定值。

Suppose c is a char declared before the code with a specific value of some sort. 假设c是在代码之前声明的char,具有某种特定值。

  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. 我或者打算为一个简单的程序使用一个正确的版本,或者不得不经历大约四倍的条件来手动打印所述字符串值,这将是一个巨大的痛苦。

  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. 我对不合逻辑的变量名表示例外,因此将fruit重命名为color ,但是这种方法是实现我认为您要问的方法的一种。

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. 最好的方法是使用switch语句,但是如果您不打算根据检测到的字符动态分配它,则首先需要告诉编译器您想要fruit[]

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. 除非您要处理具有长名称的颜色的水果,否则我会说16个字符就足以进行演示。 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 现在,关于char c的简单switch语句

    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. 请注意,我在这里使用了strcpy() ,因为我确定我将在目标的范围内进行编写。 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. 您永远都不会像在编译时那样随意复制长度未知的内容,而应使用strncpy() ,它会使用另一个长度参数。

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. 您还需要做的是将果味打印机中的c转换为高位或低位,因此您不必处理这两种情况。

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. 另一种方法是动态分配内存,但是仅使用一个简单的开关就可以最好地解决弄清楚内存应该是什么的语义。

You can use switch case to that: 您可以使用switch例:

#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);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM