• Skip to main content
  • Skip to footer

SoftwareProg.com

Software programming learning aide

  • Blog
    • Discussion
  • News
  • CS50’s Introduction to Computer Science by Harvard University on Edx
    • Week 1
    • Week 2
    • Week 3
    • Week 4
    • Week 5

Clue what is stopping the program to reach and execute: string s = get_string(“plaintext: “)

CS50 threads to aide as a supplementary resource › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2 › Clue what is stopping the program to reach and execute: string s = get_string(“plaintext: “)

Tagged: pseudocode of substitution project, substitution project

  • This topic is empty.
Log In Register Lost Password
Viewing 1 post (of 1 total)
  • Author
    Posts
  • February 13, 2022 at 9:46 am #215
    admin
    Keymaster


      Copy Code
      Copied
      Use a different Browser

      
      
      #include<stdio.h>
      #include<cs50.h>
      #include<string.h>
      #include<ctype.h>
      #include<stdlib.h>
      int main(int argc, string argv[])
      {
      int counter = 0;
          if (argc != 2)
          {
          return 1;
          }
      string key = argv[1];
      int t = strlen(key);
          if(t != 26)
          {
          return 1;
          }
          while(counter < t)
          {
          isdigit (argv[1][counter]);
          return 1;
          counter = counter + 1;
          }
      string s = get_string("plaintext: ");
      int countstring = strlen(s);//count the number of characters entered by the user as plaintext
          for(int i = 0; i <= countstring; i++)//starts with first character of plaintext till the end
          {
              if (isalpha (s))//if character is alphabetic
              {
              s= argv[1];//replace index value of alphabetic character with the same position of key array
              }
              else
              {
             s = s;//if not alphabetic, then the entered character remains the same
              }
          }
      printf("ciphertext: %s",s);
      }

       

      While I do understand that there is a lot more to do to meet the project’s requirements, still not sure why the above program though compiling fails to give any clue if I have made any progress. I just get $ prompt whatever I enter after ./substitution.

      To be specific, what is stopping the program to reach and execute:

      string s = get_string(“plaintext: “);


      Reply



      Copy Code
      Copied
      Use a different Browser

      
      
      while(counter < t)
      {
          isdigit (argv[1][counter]);   // Evaluates to true or false
          return 1;                     // Always executes
          counter = counter + 1;
      }

       

      n your while loop the value of isdigit() is not used, a true or false is just “sitting” there on the line. The “return 1” is always executed since there is no condition if the return should execute or not. You most likely meant to condition the “return” on the result of the isdigit() check 🙂


      Query


      Thanks for pointing it out. Here is how I revised:


      Copy Code
      Copied
      Use a different Browser

      
      
      while(counter < t)
      {
          if (isdigit (argv[1][counter]))
          {
          return 1;
          }
          else
          {
          counter = counter + 1;
          }
      }

       


      Reply


      Much better! Only thing is that you are only checking for digits, what you should be checking is non-alphabetic 🙂[learn_press_profile]

    • Author
      Posts
    Log In Register Lost Password
    Viewing 1 post (of 1 total)
    • You must be logged in to reply to this topic.
    Log In

    Log in / Register

    Initiative by Digital Splendid OPC Pvt. Ltd.