【问题标题】:Scan String Input for each testcase in C扫描 C 中每个测试用例的字符串输入
【发布时间】:2017-11-03 08:45:08
【问题描述】:

我的问题的目的很简单。我从用户那里得到的第一个输入是 n(测试用例的数量)。对于每个测试用例,程序将扫描用户输入的字符串。我将分别处理这些字符串中的每一个。 这里的问题是如何获取字符串输入并用 C 语言分别处理它们???这个想法类似于字典的概念,我们可以在一个大数组中包含许多单独的数组。

到目前为止我写的程序:

#include <stdio.h>
#define max 100
int main (){
int n; // number of testcases
char str [100];
scanf ("%d\n",&n);
for (int i =0;i <n;i++){
scanf ("%s",&str [i]);
}
getchar ();
return 0;
}

有人可以建议应该怎么做吗?

输入应该是这样的:

输入 1:

3

鞋子

房子

输入 2:

2

蜜蜂

这里 3 和 2 是 n 的值,即测试用例的数量。

【问题讨论】:

  • 使用不匹配的scanf 转换序列和参数会导致未定义的行为"%d" 转换是针对int 而不是char
  • 此外,您实际上并没有初始化n,这意味着它的值将是indeterminate,并且在您使用它时可能导致未定义的行为 .
  • 现在您编辑以读取一个字符串,这更好,但在您使用scanf 的上下文中似乎不正确。看来您应该在循环中阅读单个 char
  • 还有其他方法可以扫描字符串吗?
  • 如果你想得到一个字符串,要么使用fgets 读取整行,要么使用例如scanf("%s", str).

标签: c arrays string dictionary recursion


【解决方案1】:

首先,不要混淆 C++ 中的“字符串”和 C 中的“字符数组”。

由于你的问题是基于C语言的,所以我会按照那个来回答...

#include <stdio.h>
int main (){

    int n; // number of testcases
    char str [100][100] ; // many words , as individual arrays inside one big array

    scanf ("%d\n",&n);

    for (int i =0;i <n;i++){
        scanf ("%s",str[i]); // since you are taking string , not character
    }

    // Now if you want to access i'th word you can do like
    for(int i = 0 ; i < n; i++)
        printf("%s\n" , str[i]);
    getchar ();
    return 0;

}

现在这里不使用二维数组,您还可以使用一维数组并用空格分隔两个单词,并将每个单词的起始位置存储在另一个数组中。 (这是很多实现)。

【讨论】:

  • 这个程序只允许我输入一个单词,然后停止
  • 请提及输入数据
  • 对不起我的错误
  • 告诉你的意见@SteffiKeranRaniJ
  • 你做对了吗@SteffiKeranRaniJ。因为我得到了输出。
【解决方案2】:
#include <stdio.h>
#define MAX 100        // poorly named

int n=0; // number of testcases
char** strs=0;

void releaseMemory()    // don't forget to release memory when done
{
    int counter;    // a better name

    if (strs != 0)
    {
        for (counter=0; counter<n; counter++)
        {
            if (strs[counter] != 0)
                free(strs[counter]);
        }
        free(strs);
    }
}

int main ()
{
    int counter;    // a better name

    scanf("%d\n",&n);
    strs = (char**) calloc(n,sizeof(char*));
    if (strs == 0)
    {
        printf("outer allocation failed!")
        return -1;
    }
    for (counter=0; counter<n; counter++)
    {
        strs[counter] = (char*) malloc(MAX*sizeof(char));
        if (strs[counter] == 0)
        {
            printf("allocate buffer %d failed!",counter)
            releaseMemory();
            return -1;
        }
        scanf("%s",&strs[counter]);    // better hope the input is less than MAX!!
        // N.B. - this doesn't limit input to one word, use validation to handle that
    }
    getchar();

    // do whatever you need to with the data

    releaseMemory();
    return 0;
}

【讨论】:

  • 此程序在 main () 内的第 2 行显示错误。 "void 类型的值不能分配给 "char**" 类型的实体
  • 同一行中的另一个错误:函数调用中有两个参数
  • 是的,calloc() 参数列表不正确,必须强制返回。我已经更正了我的答案。
  • 顺便说一句,我使用 cntctr 作为我的前 2 个计数器名称,而不是 ij - 它们比单字母名称更容易搜索/替换,但是仍然足够短,可以输入,因此在编写代码时不会增加太多开销。此外,cntctr 暗示了“计数器”,使代码更具可读性。
【解决方案3】:

首先你的不是C程序,因为你不能在C中的FOR循环中声明变量,其次使用Pointer to Pointer创建了一个原型,将字符数组存储在矩阵式数据结构中,这里是代码:-

#include <stdio.h>
#include <stdlib.h>
#define max 100
int main (){
int n,i; // number of testcases
char str [100];
char **strArray;
scanf ("%d",&n);
strArray = (char **) malloc(n);
for (i =0;i <n;i++){
(strArray)[i] = (char *) malloc(sizeof(char)*100);
scanf ("%s",(strArray)[i]);
}
for (i =0;i <n;i++){

    printf("%s\n",(strArray)[i]);
    free((strArray)[i]);
}

getchar ();
return 0;
}

【讨论】:

  • “不能在 C 中的 FOR 循环中声明变量”的说法不正确。自 C99 以来,可以在 for 语句中声明变量。
  • 是的,这取决于您使用的编译器!我正在使用 Vs2010,它不完全符合 C99,因此对于提供的信息不足表示歉意
猜你喜欢
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 2011-03-18
  • 2014-01-26
  • 1970-01-01
相关资源
最近更新 更多