IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 2: [Arrays] – Functions, Variable and Scope, Debugging, Arrays, and Command Line Arguments › Clue what is stopping the program to reach and execute: string s = get_string(“plaintext: “)
- This topic is empty.
-
AuthorPosts
-
February 13, 2022 at 9:46 am #215::
#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
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:
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]
-
AuthorPosts
- You must be logged in to reply to this topic.