【问题标题】:Can't figure out why strcpy isn't copying string effectively?无法弄清楚为什么 strcpy 不能有效地复制字符串?
【发布时间】:2014-08-29 12:14:55
【问题描述】:

我正在尝试构建一个程序,让用户以这种格式“01'14'2013”​​输入日期,并将其输出为这种格式“2013 年 1 月 14 日”。我正在尝试将保存用户输入的字符串复制到另一个字符串上,以便稍后将其连接到原始字符串上,而无需字符串的第一个和第二个索引,这样我就只有 '/14/2013',来自原始字符串,然后将 '/' 替换为 ' ' 以便读取月份、日期和年份....但是由于某种原因,当我尝试将原始字符串从输入复制到另一个字符串(我打算稍后连接),它不能有效地复制,我错过了什么..?

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


int main() 
{ 

char date[100]; 


   char month[100]; 
   char array[12][100] ={"January", "Febuary", "March", "April", "May", "June", "July",  "August", "September", "October", "November", "December"}; 
   char month2[100]; 

   printf(" Please enter a date "); 
   fgets( date, 100, stdin); 

 strcpy(month2, month);  

 if( date[0] == '0' && date[1] == '1')
 {
   strcpy(month, array[0]); 
 }
 else if( date[0] =='0' && date[1] == '2')
 { 
   strcpy(month, array[1]);
 }
 else if( date[0] =='0' && date[1] == '3')
 { 
   strcpy(month, array[2]);
 }   
 else if( date[0] =='0' && date[1] == '4')
 { 
   strcpy(month, array[3]);
 }
 else if( date[0] =='0' && date[1] == '5')
 { 
   strcpy(month, array[4]);
 }   
 else if( date[0] == '0' && date[1] == '6')
 {
   strcpy(month, array[5]); 
 }
 else if( date[0] =='0' && date[1] == '7')
 { 
   strcpy(month, array[6]);
 }
 else if( date[0] =='0' && date[1] == '8')
 { 
   strcpy(month, array[7]);
 }   
 else if( date[0] =='0' && date[1] == '9')
 { 
   strcpy(month, array[8]);
 }
 else if( date[0] =='1' && date[1] == '0')
 { 
   strcpy(month, array[9]);
 }   
 else if( date[0] =='1' && date[1] == '1')
 { 
   strcpy(month, array[10]);
 }
 else if( date[0] =='1' && date[1] == '2')
 { 
   strcpy(month, array[11]);
 }   









 printf("%s \n", month); 
 printf("%s \n", month2); 
 return 0; 

}

【问题讨论】:

  • 你对strcpy(month2, month);有什么期望?
  • 你可能想要 strcat,因为 strcpy 只是在你每次复制到它时删除目标字符串。strcat 会追加。
  • month[12][100] 中的 100 似乎有点矫枉过正;九月有 9 个字母,所以month[12][10] 就足够大了。但是,它不会导致任何故障。当然最好将date[0]date[1] 转换为数字 (int n = (date[0] - '0') * 10 + (date[1] - '0');, and then use strcpy(月,数组[n]);, possibly after validating that the date[0]` 是 '0''1',以及 isdigit(date[1])

标签: c


【解决方案1】:
strcpy(month2, month); 

此时monthmonth2 都没有被初始化为任何有用的东西。它们的内容是不确定的,使用非正确终止的 C 字符串调用 strcpy 会调用未定义的行为。

对我来说似乎是一个错字。

【讨论】:

    【解决方案2】:

    代码较少,但没有一些验证。

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
            char date[100];
    
            char month[100];
            char array[12][100] = {"January", "Febuary", "March", "April", "May", "June", "July",  "August", "September", "October", "November", "December"};
    
            printf(" Please enter a date ");
            fgets( date, 100, stdin);
    
            char month2[100];
            strcpy(month2, date);
            month2[2] = '\0';
    
            strcpy(month, array[atoi(month2) - 1]);
    
            printf("%s \n", month);
            return 0;
    }
    

    【讨论】:

      【解决方案3】:
      #include <stdio.h>
      #include <string.h>
      #include <ctype.h>
      
      typedef struct mon {
          const char *name;
          const int len;
      } Month;
      
      #define M(x){ x " ", sizeof(x)}
      
      Month month[] = { {"", 0}, //dummy
          M("January"), M("Febuary"), M("March"), M("April"),
          M("May"), M("June"), M("July"), M("August"),
          M("September"), M("October"), M("November"), M("December")
      }; 
      
      
      int main(){
          char in_date[128];
          char out_date[128] = "";
          int m = 0, pos;
           printf(" Please enter a date E.g MM/DD/YYYY\n");
           fgets( in_date, sizeof(in_date), stdin);
      
          if(in_date[0] == '0'){
              if(isdigit(in_date[1]) && in_date[1] != '0'){
                  m = in_date[1] - '0';
                  pos = month[m].len;
                  memcpy(out_date, month[m].name, pos);
              }
          } else if(in_date[0] == '1'){
              if('0' <= in_date[1] && in_date[1] <= '2'){
                  m = 10 + in_date[1] - '0';
                  pos = month[m].len;
                  memcpy(out_date, month[m].name, pos);
              }
          }
          if(m){
              memcpy(out_date + pos, in_date + 3, 7);
              out_date[pos + 2] = ',';
              out_date[pos + 7] = '\0';
              printf("%s\n", out_date); 
          } else {
              printf("invalid month\n");
          }
          return 0; 
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-29
        相关资源
        最近更新 更多