【问题标题】:Beginner C Program - How do I return necessary output from loop in anagram program?初学者 C 程序 - 如何从 anagram 程序中的循环返回必要的输出?
【发布时间】:2015-01-13 07:18:30
【问题描述】:

感谢您查看我的问题!

我正在尝试从 C 中的教科书示例练习创建一个工作程序(我仍然是初学者,我正在通过书籍/笔记学习),它将确定用户输入的两个字符串是否是字谜(无论大小写或间距)。 为什么我的程序正在编译,但没有通过第一个 getString 提示以允许用户输入第二个字符串? 以下是练习的目标,以及到目前为止我编写的代码(在底部)。

算法 读取第一个字符串,然后编写一个循环,该循环使用 26 个整数的数组来计算有多少个整数 每个字母被看到的次数。 读取第二个字符串,这一次减少 int 数组中每个字母的计数。 如果 int 数组中的每个元素都是 0,则字符串是字谜。 忽略任何不是字母的字符。将大写字母视为与它们相同 小写等效。下面给出了这方面的帮助。

所需功能 您需要使用以下功能。您必须设计适当的参数 和返回类型。如果它们使您的程序更简单,您还可以添加其他功能 更清晰。

ma​​in() 声明变量,然后调用下面描述的函数来解决问题。

初始化() 在读取第一个字符串之前清除所有 char 和 int 数组的内容。 (将 char 数组中的每个元素设置为空字符“\0”。)

getString() 提示并从输入中读取字符串。 执行此操作时,从中调用标准库函数 gets()。功能 原型是:char *gets(char s[]);将下一个输入行读入字符串 s;它将终止的换行符替换为“\0”。 如果文件结束或发生错误,则返回 s 或 NULL。

setLetters() 循环遍历第一个字符串中的每个字符, 并更新 int 数组中该字母的计数。 调用以下标准库函数 当你这样做时。函数原型是:

int isalpha(char c) 如果字符 c 是小写或大写字母,则返回非零 (true)

char tolow(char c) 如果字符 c 为大写字母,返回对应的小写字母;否则返回 c

checkLetters() 循环遍历第二个字符串中的每个字符,并从 int 数组中该字母的计数中减去 1。与 setLetters() 非常相似。

isZero() 循环 int 数组。如果每个元素都为 0,则返回 TRUE,否则返回 FALSE。

这是我到目前为止编写的代码,它可以编译但没有到达第二个字符串:

#include <stdio.h>
#include <ctype.h>
#define ABC 26


/* lab5.c: Compares user input to determine if they are anagrams */
char initialize(char s1[], char s2[], int count[]);
char getString(char s[]);
char *gets(char s[]);
int setLetters(char s1[], int count[]);
int checkLetters(char s2[], int count[]);
int isZero(int count[]);

int main() {

    char string1[100];
    char string2[100];
    int count1[ABC] = {0};

    initialize(string1, string2, count1);

    getString(string1);
    setLetters(string1, count1);
    getString(string2);
    checkLetters(string2, count1);

    isZero(count1);

}
char initialize(char s1[], char s2[], int count[]) {
    int i;

    for (i = 0; s1[i] != '\0'; i++) {
        s1[i] = '\0';
        s2[i] = '\0';
        count[i] = 0;
    }
}
int setLetters(char s1[], int *count[]) {
    int i = 0;

    while (s1[i] != '\0') {
      if (isalpha(s1[i])) {
        count[tolower(s1[i]) - 'a']++;
        i++;
      }
   }
   return count;
}
int checkLetters(char s2[], int count[]) {
    int i;

    while (s2[i] != '\0') {
      if (isalpha(s2[i])) {
        count[tolower(s2[i]) - 'a']--;
        i++;
      }
   }
}
char getString(char s[]) {

    printf("Enter the string: ");
    gets(s);
}
int isZero(int count[]) {
    int i;
    int x = 0;

    for (i = 0; i < ABC; i++) {
        if (count[i] != 0)
            x = 1;
    }
    if (x == 1) {
        printf("Not Anagram");
    }
    else {
        printf("Anagram");
    }
}

输出:

Enter the string: mattress
Enter the string: smartest
Not Anagram

...这不是完全正确的输出。 :P

【问题讨论】:

  • 今天早些时候不是有人问过这个问题吗?
  • 您的代码有几个问题;它甚至不应该首先编译。 setLetters 被声明为返回int,但它返回一个指向int 的指针。 initialize 声明返回 char(为什么???),但它不返回任何内容。 getString 的原型与作业中列出的不同。
  • 从不,从不,从不使用 gets 由于严重的安全问题,它不再是 C 标准的一部分。请改用fgets、getline 或scanf。 (getline 提供了一些其他人不喜欢的细节,返回实际读取的字符数,如果lineptr 设置为NULL,则分配内存来保存您的输入)但是一切都会正常工作。 (这个问题已经在几个小时前回答了)
  • @paulalucai 标记您的问题已解决,press the check-mark near the answer that helped you。
  • 我已经通过解决方案回滚了您对问题的替换。请找到your solution in the revision history 并将其作为自己的答案发布。

标签: c arrays string loops


【解决方案1】:

抱歉,我想发布一个示例来替换您昨晚的 getString 函数。下面是函数fgetsinput 的简单示例,您可以使用fgets 从stdin 获取输入,这将大大提高代码的安全性/漏洞。请注意,当使用在堆栈上静态创建的存储时,#define 表示要读取的最大输入(或字符串)行的常量是正常的。下面,要读取的最大字符串大小是一个常量MAXS,最初定义为256。您可以根据需要进行调整。看到的正常值范围一般在128和2048之间

另外注意,通过将存储字符串初始化为全零(null),您将使用null-terminating 字符填充字符串,以确保您的输入将始终为@987654331 @ 除非你覆盖 MAXS-1 的最后一个 null:

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

#define MAXS 256

char *fgetsinput (char *s)
{
    fgets (s, MAXS - 1, stdin);     /* read string from stdin   */

    size_t len = strlen (s);        /* calculate length         */

    if (s[len-1] == '\n')           /* if last char is newline  */
        s[len-1] = 0;               /* set to null-terminator   */

    return s;                       /* return adds flexibility  */
}

int main (void) {

    char line[MAXS] = {0};                  /* initialize to zero (null)    */

    while (strcmp (line, "quit") != 0)
    {
        printf ("\n enter a string : ");
        fgetsinput (line);                  /* return not used in this call */
        printf (" string in line : %s\n", line);
    }

    printf ("\n enter a string to assign to pointer  : ");

    char *p = fgetsinput (line);            /* return assinged to pointer   */
    printf (" What is the value pointed to by 'p'? : %s\n\n", p);

    return 0;
}

输出:

$ ./bin/getinputex

  enter a string : A string for fgets to read.
  string in line : A string for fgets to read.

  enter a string : quit
  string in line : quit

  enter a string to assign to pointer  : A string for fgets to read and assign.
  What is the value pointed to by 'p'? : A string for fgets to read and assign.

通过将文件描述符作为参数之一传递给函数,可以轻松地将函数扩展为从任何文件流中读取。 (例如char *fgetsinput (char *s, FILE *fp))。然后,您可以将函数中的 stdin 替换为 fp 并从任何打开的文件流中读取。

【讨论】:

  • 感谢大卫的所有建议!我已经根据练习说明更新了我的代码以输出正确的结果。 :)
  • 很好,这里有很多聪明人很乐意提供帮助。当你继续学习 C 时,关键是——放慢速度。 C是一种“精确”的语言。这是一项巨大的资产,并且是任何能够让您对硬件进行如此巨大的低级控制的语言的要求。每个陈述的每个部分都很重要。其他语言可能允许“足够接近”的广泛语法。对于 C,它要么是“正确的”,要么是“错误的”。多花一点时间学习 C 是值得的。
  • 是的,有时我对这种语言的“精确”程度感到非常害怕,我觉得一切都必须手动计算;我会遵循你的智慧之言,谢谢!有一个伟大的一周大卫! :)
猜你喜欢
  • 2013-06-28
  • 1970-01-01
  • 2014-12-21
  • 1970-01-01
  • 2021-07-22
  • 2014-11-04
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多