【问题标题】:String Compare issues in CC中的字符串比较问题
【发布时间】:2014-02-11 07:05:47
【问题描述】:

所以作业是这样的:问题 C:练习字符串(姓氏.c)(8 分)

读入 n 个,然后读入 n 个姓氏,并检查列表中的第一个是否再次重复。

示例运行 #1

输入 n,后跟 n 姓氏(每个姓氏必须是一个单词):

5 里根·布什·克林顿·布什·奥巴马

列表中的名字不重复。

示例运行 #2

输入 n,后跟 n 姓氏(每个姓氏必须是一个单词):

4 布什·克林顿·布什·奥巴马

列表中的名字重复。

我可以比较前两个名称,但我不知道如何将第一个名称与第二个字符串数组中的名称进行比较。如果有人搜索并复制我的代码,我不想发布我的代码。不过我会发给你的。任何帮助将不胜感激。

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

int main(void) 
{   
// initializing character strings
char last[25], first[25];

// initializing number of names, and the index for the number of names
int index, n;

// read in the number
printf("Enter n, followed by n Last names (each last name must be a single word) :\n");
scanf("%d", &n);
scanf("%s", first);

for (index = 0; index < n; index++) 
    scanf("%s", last);

if (strcmp(last, first)== 0)
{
    printf("First name in list is repeated.\n"); 
}
else 
{
    printf("First name in list is not repeated.\n");
}
return 0;
}

【问题讨论】:

  • 预计问题会伴随着该人迄今为止尝试过的演示文稿。这包括源代码。考虑到您甚至还没有解决问题,因此任何人都不太可能复制它。
  • 告诉我们你做了什么,然后寻求帮助。在 StackOverflow 中,每个人都喜欢互相帮助并分享他们的知识。要求做你的任务是不对的:(
  • 与基础 C 语言课程的问题-c 的不完整解决方案相比,有更多有趣和重要的代码片段可以从万维网的漏洞中撕下。如果您想知道自己做错了什么,请发布您的代码。
  • 我不想发布它的原因是我班上没有其他人会复制它。但我想我会冒这个风险
  • @ThanushanBalakrishnan 有什么想法吗?

标签: c string for-loop strcmp


【解决方案1】:

在您的forloop 中,lastvariable 每次都会被覆盖,这意味着您只是将第一个输入与下一个n 姓氏序列中的姓氏进行比较。

如果要计算重复次数,请在 for 循环中使用计数器。

int nRepetitions = 0;

/* ... read the numbers and the first string 
      (therefore index should start with 1)... */

for (index = 1; index < n; index++) {
    scanf("%s", last);
    if (strcmp(last, first) == 0) {
        nRepetitions++;
    }
}

【讨论】:

  • 好的,所以使用二维数组我可以解决这个问题。我仍然不明白如何实现二维数组。
  • 在这种情况下不需要二维数组。只需在第一个循环中添加一个重复计数器。另请注意,索引应从 1 开始。
【解决方案2】:
#include <stdio.h>
#include <string.h>

int main(void){
    char last[25], first[25];
    int index, n, repeated = 0;

    printf("Enter n, followed by n Last names (each last name must be a single word) :\n");
    scanf("%d", &n);
    scanf("%s", first);

    for(index = 1; index < n; index++){ //index = 1 : aleady input first
        scanf("%s", last);
        if(strcmp(last, first)== 0)
            repeated = 1;
    }
    if(repeated)
        printf("First name in list is repeated.\n");
    else 
        printf("First name in list is not repeated.\n");

    return 0;
}

【讨论】:

    【解决方案3】:

    您的代码不正确.. For 循环 last 只有一维字符数组,并且每次都会被覆盖。 所以改用二维数组..

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void) 
    {   
    // initializing character strings
    char first[25];
    
    // initializing number of names, and the index for the number of names
    int index=0, n;
    
    // read in the number
    printf("Enter the number of last names :\n");
    scanf("%d", &n);
    printf("Enter the first name \n");
    scanf("%s", first);
    
    char last[n][25];
    for (int i = 0; i < n; i++) 
        scanf("%s", last[i]);
    
    for(int i=0;i<n;i++)
    {
        if (strcmp(last[i], first)== 0)
            index++;
    }
    if(index==0)
        printf("First name not repeated\n");
    else
        printf("First name repeated %d times", index);
    
    return 0;
    }
    

    【讨论】:

    • 嗯,我明白你所说的使用二维数组的意思,但我还是不明白。
    • index == 0 因为它的增量而永远不会呈现 true,这就是我被卡住的原因。
    • 如果没有匹配则为零。检查compileonline.com/compile_cpp11_online.php
    猜你喜欢
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    相关资源
    最近更新 更多