简体   繁体   English

如何计算结构中的字符字符串?

[英]How can i count a char string in a struct?

Is there a way to count how many times a name appears in the struct?有没有办法计算名称在结构中出现的次数?

This is basically what i am trying to make: Create a program that will determine the number of times a name occurs in a list.这基本上就是我想要做的:创建一个程序来确定名称在列表中出现的次数。

The user will enter a name that appears then the output is the number on how many times the name entered appear in the list.用户将输入出现的名称,然后 output 是输入的名称出现在列表中的次数。

#include<stdio.h>

typedef struct Person {
    char name[50];
} Person;

void print_people(const Person *people) {
    for (size_t i = 0; people[i].name[0]; ++i)
        printf("%-20s \n", people[i].name);
}

int main() {

    Person people[] = { {"Alan"}, {"Bob"}, {"Alan"}, {"Carol"}, {"Bob"}, {"Alan"} };
    print_people(people);
    char name;

    return 0;

}

First of all, your function print_people has a bug.首先,您的 function print_people有一个错误。 The line线

for (size_t i = 0; people[i].name[0]; ++i)

is wrong.是错的。 It will read from the array out of bounds, because the loop condition people[i].name[0] is wrong.它将从数组中读取越界,因为循环条件people[i].name[0]是错误的。 This loop condition would only be correct if the array were terminated by an element which has name[0] set to '\0' .仅当数组被name[0]设置为'\0'的元素终止时,此循环条件才是正确的。 This was the case in the original version of your question, but not in your current version.在您的问题的原始版本中就是这种情况,但在您当前的版本中不是。

Therefore, you should change the line to the following:因此,您应该将该行更改为以下内容:

for (size_t i = 0; i < 6; ++i)

In order to count the number of people with a certain name, all you have to do is traverse the array in the same way as you are doing it in the function print_people and count the number of times you encounter the name you are searching for:为了计算具有特定名称的人数,您所要做的就是以与在 function print_people中相同的方式遍历数组并计算遇到您正在搜索的名称的次数:

int count_person( const Person *people, const char *target ) {

    int count = 0;

    for ( int i = 0; i < 6; i++ )
        if ( strcmp( people[i].name, target ) == 0 )
            count++;

    return count;
}

Your entire program would then look like this:您的整个程序将如下所示:

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

typedef struct Person {
    char name[50];
} Person;

void print_people(const Person *people) {
    for (size_t i = 0; i < 6; ++i)
        printf("%-20s \n", people[i].name);
}

int count_person( const Person *people, const char *target ) {

    int count = 0;

    for ( int i = 0; i < 6; i++ )
        if ( strcmp( people[i].name, target ) == 0 )
            count++;

    return count;
}

int main() {

    Person people[] = { {"Alan"}, {"Bob"}, {"Alan"}, {"Carol"}, {"Bob"}, {"Alan"} };

    printf( "Number of occurances of \"Alan\": %d\n", count_person( people, "Alan" ) );

    return 0;
}

This program has the following output:这个程序有以下output:

Number of occurances of "Alan": 3

You might want a function like this:您可能需要这样的 function:

int count_person(const Person *people,const Person who) {
    int whocount = 0;

    for (size_t i = 0; people[i].name[0]; ++i)
        if (strcmp(people[i].name,who.name) == 0)
            whocount++;
    return whocount;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM