• This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #176
    admin
    Keymaster
      
      
      #include<stdio.h>
      #include<ctype.h>
      #include<cs50.h>
      #include<string.h>
      int main(int 2, string argv[3])
      {
      string name = argv(2);
      printf("Entered text by user: %s\n", name);
      int n = strlen(name);
      printf("length of entered text: %i\n", n);
      int key = argv(1);
      //char newuppercase_array[n];
          for (int i = 0; i < n; i++)
          {
              if (isupper(name))//if (isupper(name))
              {
              name = ((((name - 65) + key)%26) + 65);//newuppercase_array = ((((name - 65) + key)%26) + 65);
              }
              else if (islower(name))
              {
              name = ((((name - 97) + key)%26) + 97);//newuppercase_array = ((((name - 97) + key)%26) + 97);
              }
              else
              {
              name = name; //newuppercase_array = name;
              }
              printf("%c",name);// printf("%c",newuppercase_array);
              }
              printf("\n");
      }

      nt main(int 2, string argv[3])

      Is my formatting correct for the Caesar program that will have two inputs from the user at command line interface, resulting in three argv[3].

      argv[0] will be the command ./casear (as entered by the user while running the program).

      argv will be the key entered by the user on command line interface

      argv[2] will be the plain text entered by the user on command line interface.


      Reply


      Also note that subscripting into an array is done with square brackets:

      
      
      int main(int 2, string argv[3])

      One thing you may be missing here is that you are writing a function (main) that will be passed some parameters. You don’t know, when you write the code, what will be passed. You don’t know that it will be 2 for argc (and it’s really 3, but you don’t know that either).

      You define the function to take a parameter, and at run time that variable is filled in for you. Your code can then examine the value and make decisions based on that.

      
      
      int main(int argc, string argv[])

      So here you have named the parameter argc, and it will be assigned a value by the system just before your program runs. You can think of it as the command line calling your main() function.


      Memory for argv automatically allocated by the compiler (correct me if I’m wrong) you don’t need to allocate space yourself. Just leave the brackets empty without space. Besides, why is there 2 after int? You’ve to initialise a variable there which will store the number of arguments passed to the program… C variables can only start with letters or underscores, conventional name for it is argc though you can name it whatever you want


      As all other comments have said, I’ll repeat with less jargon.

      When you create a function with parameters, do you give it hard coded literal values? Or give it the data type (which you did) and a “variable place holder”? (Which you did not).

      argc can be any number and argv can have any size.

      You would write:

      int addSum(int x, int y)

      as opposed to:

      int addSum(int 2, int 3)

      Didn’t want to use int main as an example since the others already did. But they basically gave you the answer.

      [learn_press_profile]

    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.
    Scroll to Top