修改程序以在C中作为命令行参数运行

时间:2019-02-25 06:21:55

标签: c command-line

完全公开,这是我要做的一堂课的作业。我们有一个程序来检查两个词是否是字谜。我们应该对其进行修改,以便可以在程序中将单词作为命令行输入。例如:((/ .. a.out hello elloh:是一个字谜...。// a.out hello world:不是一个字谜)。

这是原始程序:

#include <stdio.h>
#define N 26

int main()
{


    char ch;
    int letter_counts[N]= {0};
    int i;
    int count =0;

    printf("enter a word: ");

    while((ch=getchar())!= '\n')

    {
        letter_counts[ch - 'a']++;

    }

    for(i =0;i<N;i++)
        printf("%d", letter_counts[i]);


    printf("enter the second word: ");
    while((ch=getchar())!= '\n')

    {
        letter_counts[ch - 'a']--;

    }

    for(i =0;i<N;i++)
        printf("%d", letter_counts[i]);

    for(i =0;i<N;i++)
        if(letter_counts[i]==0)
            count++;


    if(count == N)
        printf("The words are anagrams.\n");
    else

        printf("The words are NOT anagrams.\n");


    return 0;
}       

现在这是我到目前为止所拥有的:

#include <stdio.h>
#define N 26

/*
  This program is a modified version of anagram.c so that the words run as command-line arguments.   
*/
int main(int argc, char *argv[])
{
  if(argc != 3)
  {
    printf("Incorrect number of arguments");
    return 0;

  }
  char ch;
    int letter_counts[N]= {0};
    int i;
    int count =0;
  //int k;
  //for (k = 1; i < argc; i++) 
  //{
    while((ch=getchar())!= '\n')
      {
          letter_counts[ch - 'a']++;
    }

      for(i =0;i<N;i++)
          printf("%d", letter_counts[i]);

      while((ch=getchar())!= '\n')  
      {
          letter_counts[ch - 'a']--;
      }

      //for(i =0;i<N;i++)
        //printf("%d", letter_counts[i]);

      for(i =0;i<N;i++)
          if(letter_counts[i]==0)
              count++;

    int k;
    int j;
    for (k = 1; i < argc; i++)
    {
      for (j = 0; j < N; j++)
      {
        if (count == N) 
        {
          printf("%s and %s are anagrams\n", argv[k], argv[k + 1]);
          break;
        } 
        else
          printf("The words are NOT anagrams. \n");
      }
    }

    if(count == N)
          printf("The words are anagrams.\n");
     else
        printf("The words are NOT anagrams.\n");
  //}




    return 0;
}       

输出(如果参数数目正确)始终为:

0000000000000000000000000
0000000000000000000000000
These are anagrams

我在这里做错了什么,最好的方法是什么?

非常感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

您正在使用从STDIN读取的getchar(),如果要从命令行参数中获取您的单词,这不是您想要的。相反,您想看看argv

for (char *c = argv[1]; *c != NULL; c++) {
    letter_counts[*c - 'a']++;
}

for (char *c = argv[2]; *c != NULL; c++) {
    letter_counts[*c - 'a']--;
}

有关argcargv的更多信息:http://crasseux.com/books/ctutorial/argc-and-argv.html

答案 1 :(得分:0)

为了不为您解决家庭作业,我将向您展示如何在其他程序上使用argcargv,该程序仅检查第一个参数是否与第二个参数相反:

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

int main(int argc, char **argv)
{
    if (argc != 3) {
        printf("Usage: %s first_word second_word\n\n", argv[0]);
        return EXIT_SUCCESS;
    }

    char const *first = argv[1];
    char const *second = argv[2];

    size_t first_length = strlen(first);
    size_t second_length = strlen(second);

    if (first_length != second_length) {
        puts("The words are NOT semordnilaps.\n");
        return EXIT_SUCCESS;
    }

    for (size_t first_index = first_length, second_index = 0; first_index; --first_index, ++second_index) {
        if (first[first_index - 1] != second[second_index]) {
            puts("The words are NOT semordnilaps.\n");
            return EXIT_SUCCESS;
        }
    }

    puts("The words are semordnilaps.\n");
}

答案 2 :(得分:-1)

修改后的程序从命令行运行。以下是适合您的工作代码段。 在这里,我们传递两个带有程序名称的命令行参数。 while((ch = argv [1] [len])!='\ 0')从第一个参数按字符检索char,而while(((ch = argv [2] [len])!='\ 0')检索与第二个逻辑相同,其余逻辑保持不变。

#include <stdio.h>
#define N 26

int main(int argc, char *argv[])
{
    if( argc != 3)
    {
        printf("Incorrect argumemts\n");
        return 0;
    }
    char ch;
    int letter_counts[N]= {0};
    int i;
    int count =0;

    int len=0;
    while ((ch = argv[1][len]) != '\0')
    {
        letter_counts[ch - 'a']++;
        len++; /* moving index */
    }

    for(i =0;i<N;i++)
        printf("%d", letter_counts[i]);

    len=0;
    while ((ch = argv[2][len]) != '\0')
    {
        letter_counts[ch - 'a']--;
        len++; /* moving index */
    }

    for(i =0;i<N;i++)
        printf("%d", letter_counts[i]);

    for(i =0;i<N;i++)
        if(letter_counts[i]==0)
            count++;

    if(count == N)
        printf("The words are anagrams.\n");
    else

        printf("The words are NOT anagrams.\n");


    return 0;
}