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 › Problem understanding array with Caesar project
Tagged: arrays, Caesar project
- This topic is empty.
-
AuthorPosts
-
February 4, 2022 at 10:34 am #192
[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
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)
[/dm_code_snippet]
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[dm_code_snippet background=”yes” background-mobile=”no” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
argv[1][0] = 4 argv[1][1] = 5 argv[1][2] = 0 argv[1][3] = R
[/dm_code_snippet]
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][i] has the record of its length.
int counter = 0
for (argv[1][i] = 0; argv[1][i] != ‘/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
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
int len; for (len = 0; argv[1][len] != '\0'; len++) ;
[/dm_code_snippet]
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
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”yes” line-numbers=”yes” bg-color=”#abb8c3″ theme=”dark” language=”clike” wrapped=”yes” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
int len = strlen(argv[1]);
[/dm_code_snippet]
Take a look at this for a more complete example
[dm_code_snippet background="yes" background-mobile="yes" slim="yes" line-numbers="yes" bg-color="#abb8c3" theme="dark" language="clike" wrapped="yes" height="" copy-text="Copy Code" copy-confirmed="Copied"]
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); }
[/dm_code_snippet]
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.
-
argv is an array of strings. What are strings? Array of chars.
-
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][i] has the record of its length.
int counter = 0
for (argv[1][i] = 0; argv[1][i] != ‘/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]
-
-
AuthorPosts
- You must be logged in to reply to this topic.