【发布时间】: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