[英]Searching for string in char array of strings in C
char names [MAX_CLASS_SIZE][MAX_NAME_SIZE+1]={"Julias Hoffman","Dianne Conner","Mitchell Cooper","Johnnie Greene","Johanna Mason","Kevin Adkins","Brandi Spencer","Marian Tyler","Chester Cross","Martin Lawrence","Jane Smith","Sara Jones"};
char specificName[]="";
int search(char names[][MAX_NAME_SIZE+1],char specificName[])
for(i=0;i<MAX_CLASS_SIZE;i++){
if (strcmp(names[i],specificName)==0)
{
printf("Found %s",names[i]);
return i;
}
}
This function receives an array of strings, called name and an array of characters that represents a specific name. 此函数接收一个称为名称的字符串数组和一个代表特定名称的字符数组。 This function searches through the array name for the specific name and returns the index of the specific name if it is found, -1 if it is not found. 此函数在数组名称中搜索特定名称,如果找到,则返回该特定名称的索引;如果找不到,则返回-1。
Couldnt get the arrays to format properly my apologies. 无法让阵列正确格式化我的歉意。 In main I ask the user for a name and store it in the specificName char array. 在主要方面,我要求用户输入名称并将其存储在specificName char数组中。 Then in the search function I am attempting to search the array of names for the user given name and return a certain value based on if the name is found. 然后在搜索功能中,我尝试搜索给定名称的用户的名称数组,并根据是否找到该名称返回某个值。 I have included the necessary libraries. 我已经包括了必要的库。 I am trying to do this using strcmp but it does not seem to work properly. 我正在尝试使用strcmp来执行此操作,但是它似乎无法正常工作。 Any help is greatly appreciated! 任何帮助是极大的赞赏!
Took a quick look and it seems to work fine. 快速浏览一下,它似乎工作正常。 Your i needs to be declared as an int but otherwise, the following works: 您需要将i声明为int,否则,可以进行以下工作:
#include <stdio.h>
#include <stdlib.h>
#define MAX_CLASS_SIZE 100
#define MAX_NAME_SIZE 100
int strcmp(const char *, const char *);
int main(int argc, const char * argv[]) {
char names [MAX_CLASS_SIZE][MAX_NAME_SIZE+1]={"Julias Hoffman","Dianne Conner","Mitchell Cooper","Johnnie Greene","Johanna Mason","Kevin Adkins","Brandi Spencer","Marian Tyler","Chester Cross","Martin Lawrence","Jane Smith","Sara Jones"};
char specificName[]="Brandi Spencer";
int search(char names[][MAX_NAME_SIZE+1],char specificName[]);
for(int i=0;i<MAX_CLASS_SIZE;i++){
if (strcmp(names[i],specificName)==0){
printf("Found %s",names[i]);
return i;
}
}
return 0;
}
Output 输出量
Found Brandi Spencer 发现布兰迪·斯宾塞
My guess would be is that you are not passing the user input correctly. 我的猜测是您没有正确传递用户输入。 Check specificName[] prior to strcmp(). 在strcmp()之前检查specificName []。
You could try the following approach: 您可以尝试以下方法:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int search(char **names,char *specificName, size_t length){
size_t i,found=0;
for(i=0;i<length;i++){
if (strcmp(*(names + i),specificName)==0){
found = 1;
}
}
return found == 1 ? 0 : 1;
}
int main(void) {
size_t length;
char specificName[100];
char *names[12]={"Julias Hoffman","Dianne Conner","Mitchell Cooper","Johnnie Greene",
"Johanna Mason","Kevin Adkins","Brandi Spencer","Marian Tyler",
"Chester Cross","Martin Lawrence","Jane Smith","Sara Jones"};
length = sizeof names / sizeof *(names + 0);
printf("Type a name to be searched:> ");
if( fgets (specificName, 100, stdin) == NULL){
printf("Error!\n");
}
specificName[strcspn(specificName, "\n")] = 0;
if((search(names,specificName,length)) == 0){
printf("Found %s",specificName);
}else{
printf("There was no Record Found.\n");
}
return 0;
}
Output: 输出:
Type a name to be searched:> Sara Jones
Found Sara Jones
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.