【问题标题】:about 'strcpy' function using关于'strcpy'函数使用
【发布时间】:2013-09-26 07:42:31
【问题描述】:
char input[32];
char name[32];
char discountUser[32];//not sure to using about arrays.
char notDiscountUser[32];//not sure to using about arrays.
int i,j;
int len;
fgets(input,32,stdin);
sscanf(input,"%s",name);
len = strlen(name);
for(i=0,j=0; i < len; i++)
{
if(isdigit(name[i]))
{
    digits[j] = name[i];

    if (digits[j] > 48)
    {
        strcpy(discountUser[i],name); //i want to stored the name at i index
        printf("you have discount code\n");
    }
    else if (digits[j] <= 48)
    {
        strcpy(notDiscountUser[i],name); //i want to stored the name at i index
        printf("you don't have discount code\n");
    }
    j++ ;
}
}

我需要区分是否有折扣码的用户 通过输入 3charofname 和 1 位数字,例如。猫2 如果数字大于0,则用户有折扣 如果 digit 是 0 所以,他们没有折扣 例如我有 cat0 bee1 ear2 eye0 当我打印 不打折 : cat0 , eye0 折扣 : bee1 , ear2

我通过 isdigit 检查数字,我在通过 strcpy 复制用户名时遇到问题。 感谢帮助 。 :]

【问题讨论】:

  • strcpy前需要加**什么?
  • BTW char input[32] 是一个数组,char discountUser[32][32] 是一个矩阵。所以 32 列 32 行
  • 什么是数字[j]?我认为您还需要阅读 strcpy 的工作原理:cplusplus.com/reference/cstring/strcpy

标签: c arrays strcpy


【解决方案1】:

使用二维数组:

char discountUser[N][32];  
char notDiscountUser[N][32];  

其中N 是最大用户数,您可以将#define 设置为某个值。

你想要做的是:

char discountUser[32];

这是一个字符串,如果您使用char discountUser[i],您指的是字符串discountUser 中索引i 处的char(单个字符,而不是字符串)。

现在,strcpy 需要一个 字符串 作为其两个参数的输入,因此您不能将 discountuser[i] 作为其输入。

当你声明一个二维数组时,discountuser[i] 将引用 string(实际上 dicountuser[i] 将充当 char 指针),所以现在你可以将其作为参数传递给strcpy

【讨论】:

    猜你喜欢
    • 2012-09-15
    • 2021-08-12
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    相关资源
    最近更新 更多