简体   繁体   中英

Modifying a program to run as command line arguments in C

Full disclosure, this is an assignment for a class I have to do. We have a program that checks if two words are anagrams. We are supposed to modify it so that we can enter the words as command lines in a program. For instance: (./a.out hello elloh: is an anagram... ./a.out hello world: Not an anagram).

Here is the original program:

#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;
}       

Now here is what I have so far:

#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;
}       

The output (if the number of arguments is correct) is always :

0000000000000000000000000
0000000000000000000000000
These are anagrams

What am I doing wrong here and what is the best way to go about this?

Thank you for any help I really appreciate it.

You are using getchar() which reads from STDIN, which isn't what you want to do if you're getting your words from command-line arguments. Instead you want to look at argv :

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

and

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

More about argc and argv : http://crasseux.com/books/ctutorial/argc-and-argv.html

To not solve your homework for you I'll show you how to use argc and argv on a different program that just checks if the first parameter reversed equals the second:

#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");
}

Modified program torun from command line argumement. Below is working code snippet for you. Here we pass two command line arguments with program name. while ((ch = argv[1][len]) != '\\0') retrieve the char by char from first arguments and while ((ch = argv[2][len]) != '\\0') retrieve the same from the second and rest of the logic remains same.

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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