【问题标题】:C - adding a string to an existing arrayC - 将字符串添加到现有数组
【发布时间】:2016-01-22 10:24:45
【问题描述】:

我正在尝试向现有数组添加一个字符串,但我有点卡住了。我必须检查 *word 是否已经在数组中,如果不是,那么我必须将它添加到数组中。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
int insert(char *word, char *Table[], int n)
{
    //char *word is the string to be added, *char Table[] is array of strings
    //int n is the return value, which is the number of elements in *Table[]
    int counter = 0
    while(*Table[counter]) //while at Index counter
    {
        if strcmp((*Table[counter], *word) == 0) //if *word at Index counter is true
        {
            return n; //return the amount of strings in the array
            break; //terminate the while-loop
        else
        {
            counter++; //increment the counter to check the next index
        }
    }
}

所以我希望成功检查 *word 是否已经在数组中,但如果不是,我该如何添加它?在此先感谢各位。

【问题讨论】:

    标签: c arrays while-loop


    【解决方案1】:

    首先在你拥有的地方

    if strcmp((*Table[counter], *word) == 0) //if *word at Index counter is true
            {
                return n; //return the amount of strings in the array
                break;
    

    break 没用,因为一旦你return n,该功能就会停止。

    其次让我们考虑一下,如果我们在*Table[] 中找不到单词,那么while 循环应该最终退出吧?所以我们找不到*word的情况是如果我们退出while循环

    编辑:你的编译器应该抱怨这个函数,因为它可能你退出 while 循环,然后函数不返回任何东西,但你指定它返回 int 如果你的编译器没有说任何你需要打开编译器警告或使用不同的编译器。

    尝试类似的方法。这是未经测试的代码,因此您可能仍需要使用它。

    while(*Table[counter]) //while at Index counter
        {
            if strcmp((*Table[counter], *word) == 0) //if *word at Index counter is true
            {
                return n; //return the amount of strings in the array
                break; //terminate the while-loop
            else
            {
                counter++; //increment the counter to check the next index
            }
        }
        *Table[counter] = *word; //Add word to the next available spot in the array
        return 0;
    }//End function
    

    【讨论】:

    • 如果我们永远找不到这个词,那么我想将该词添加到数组中。我该怎么做呢?
    • @donutjuice 好吧,您需要确保阵列尚未满。然后,您需要在函数末尾添加一条语句,例如 *Table[counter] = *word。再看看我的回答我添加了另一个问题的编辑
    • 那你会在我的代码中改变什么?我无法理解您的解释。我是 C 语言的初学者...
    • *Table[counter]char,因此不是 strcmp 的有效参数,它需要一个指针。如果没有足够的经验能够编写正确的代码而无需测试,您应该在发布之前真正测试您的代码。
    【解决方案2】:

    请试试这个,它运行良好并添加到字符串数组中。 对于初学者来说,最好从简单的开始。 在这里我们开始我们 5 个长度为 99 的字符串 我给你留下了一些工作要做。玩得开心。

    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXSTRINGS 5
    #define LENSTRING 99
    
    int insert( char word[], char Table[][99] )  // removed n , could not understand the need.
    {
      int counter = 0;
    
      while ( counter < MAXSTRINGS && strlen( Table[counter] ) )    //while at Index counter and string not empty
      {
    
        if ( strcmp( Table[counter], word ) == 0 )  //if string was found
        {
          return counter;           //return string position  in array of strings
          break;            //terminate the while-loop
        }
        else
        {
          counter++;        //increment the counter to check the next index
        }
    
      }// endwhile
      strcpy( Table[counter], word );
      printf( "\n added at %d", counter );
      return(counter);
    }
    
    int main(  )
    {
      char Table[MAXSTRINGS][LENSTRING];
      int i = 0;
      int answer = 0;
    
      for ( i = 0; i < MAXSTRINGS; i++ )
        memset( Table[i], '\0', LENSTRING );
    
      strcpy( Table[0], "a test" );
      strcpy( Table[1], "another test" );
    
      answer = insert( "test", Table);
      if ( !answer )
        printf( "\n already in file" );
      else
        printf( "\nadded record to position %d in array ", answer );
    
      getchar(  );
    
    }
    

    【讨论】:

    • 为什么在return语句后有break;
    • = { 0 }; 不是 memset 循环,更简单,更不容易出错
    • 如果表已满,您的代码将注销表的末尾
    • if ( !answer ) 测试很难理解,因为012 等都意味着字符串已经在给定位置的表格中
    猜你喜欢
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 2017-09-16
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    相关资源
    最近更新 更多