• This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #192
    admin
    Keymaster
      
      
      int main(int argc, string argv[])
      {
      string enteredtext = get_string("plain text:");
      printf("Entered text by user: %s\n", enteredtext);
      int n = strlen(enteredtext);
      printf("length of entered text: %i\n", n);
      string key = argv[1];
      int counter = int argv[0];
      while (counter != '\0'; counter + counter + 1)

      With argv[1], an array is formed. Now argv[1] is apparently a location with a space. This location space will contain whatever key entered by the user. We need not worry about the length of the array of argv[1] as first the user will enter the key, and based on the key, the length of the array argv[1] is determined.

      I am not sure how to denote individual elements of the array argv[1]. Suppose the user enters as key 450R.

      argv[0] = 4

      argv[1] = 5

      argv[2] = 0

      argv[R] = R

      There is an overlap as argv[1] supposed to be

      string key = argv[1]


      Reply


      argv is effectively an array of arrays

      
      
      argv[1][0] = 4
      argv[1][1] = 5
      argv[1][2] = 0
      argv[1][3] = R
      


      Query


      Now, I want to focus on the argv[1] which contains the key entered by the user.

      I want to count the number of characters of key entered.

      Once the user enters the key, its length is determined by:

      string key = argv[1];

      That is, argv[1] has the record of its length.

      int counter = 0

      for (argv[1] = 0; argv[1] != ‘/0’; i++)

      {

      counter = counter + 1

      }

      I understand it is not counter but isalpha which will be relevant for Caesar project but just for the sake of learning, am I correct in the above code.


      Reply


      Almost. You can count characters like this

      
      
      int len; 
      for (len = 0; argv[1][len] != '\0'; len++)
          ;


      In your code, you assign 0 to the first character, and you use /0 which is forward slash 0 instead of backslash 0.

      Although, the easiest way is to use the standard library function called strlen

      
      
      int len = strlen(argv[1]);


      Take a look at this for a more complete example

      int main(int argc, string argv[])
      {
          string enteredtext = get_string("plain text: ");
          printf("Entered text by user: %s\n", enteredtext);
      
          int n = strlen(enteredtext);
          printf("length of entered text: %i\n", n);
      
          string key = argv[1];
      
          int counter = 0;
          while (key[counter] != '\0')
          {
              counter = counter + 1;
          }
          printf("length of key: %i\n", counter);
      }
      
      
      


      Query


      while (key[counter] != ‘\0’)

      For the above line, can key be replaced with:

      argv[1]


      Reply


      Yes


      Query


      Can I also count the length of string by this:

      int len = strlen(key)

      given the fact that:

      string key = argv[1];


      Reply

      Yes, that is one way to do it.


      1. argv is an array of strings. What are strings? Array of chars.

      2. argv contains the number of arguments passed to the program. Though, you can, technically, view as the “length” of argv array.

      Hint: argc – 1 will give you the length of argv array. Why? Remember, argv & argc both count the program’s name.


      Query

      Now, I want to focus on the argv[1] which contains the key entered by the user.
      I want to count the number of characters of key entered.
      Once the user enters the key, its length is determined by:
      string key = argv[1];
      That is, argv[1] has the record of its length.
      int counter = 0
      for (argv[1] = 0; argv[1] != ‘/0’; i++)
      {
      counter = counter + 1
      }
      I understand it is not counter but isalpha which will be relevant for Caesar project but just for the sake of learning, am I correct in the above code.


      Reply


      Why do you want to calculate the length of the key? It is just going to be a positive integer, albeit, you will have to convert it into an int first. Look at manual.cs50.io for useful libraries & functions you can use to achieve the same.

      Besides, to answer your question, there is a useful function to calculate the length of string implemented in string.h library.It is called strlen. Explore the manual pages for more useful libraries.

      Your logic is almost correct but your approach is not. for loop syntax is incorrect. It seems, you wanted to implement the check in an if statement.

      Edit– I frogot to mention, while loop allows you to iterate until/unless a specific condition is met. It will be more suitable here.

      [learn_press_profile]

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