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

      The goal is to convert data type of key variable from string type to int type so that from command line interface, key is string type, but for computing from plain text to cipher text, key is converted to data type int.

      I tried in the above code wtih:

      atoi(key);

      Also tried with

      atoi(argv[1])

      Both did not compile. Seeking help as it appears my concept is not clear.


      Reply

      You can’t change the data type of a variable in C. atoi is a function that takes in a string and returns the equivalent number. You need to store that return value in a new variable of the correct type:

      
      
      int shift = atoi(argv[1]);


      Query


      Can it be:

      int shift = atoi(enteredtext);

      It is confusing.


      Reply

      Is enteredtext a string containing a number that you want to convert to an actual number?

      [learn_press_profile]

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