First I would like to thank everyone for everything they have ever done for me whether they knew it or not. I am a first time poster long time lurker.
I am starting a new class that is based around C and several of its variants. The problem is it assumes you have experience with at least C++, and I unfortunately have only taken a couple semesters of Java and Mips. I am struggling to learn C right now with no book or lesson plan going over it. I have survived this first month using Google. But my question today I just cannot seem to wrap my head around, and while I know it is elementary I would like some help understanding the working of the code in my assignment.
The professor has supplied us with the following code:
#include <stdio.h>
#include <string.h>
void encrypt(int offset, char *str) {
int i,l;
l=strlen(str);
printf("\nUnencrypted str = \n%s\n", str);
for(i=0;i<l;i++)
if (str[i]!=32)
str[i] = str[i]+ offset;
printf("\nEncrypted str = \n%s \nlength = %d\n", str, l);
}
void decrypt(int offset, char *str) {
}
void main(void) {
char str[1024];
printf ("Please enter a line of text, max %d characters\n", sizeof(str));
if (fgets(str, sizeof(str), stdin) != NULL)
{
encrypt(5, str); // What is the value of str after calling "encrypt"?
// add your method call here:
}
}
So the questions for the homework are listed in the code, but to make it clear that is not what I am after. I want to understand how this program is working so far.
Specifically:
-
Why write
char str[1024] -
What exactly is
if (fgets(str, sizeof(str), stdin) != NULL)doing? I have a decent idea but I don’t know the reasoning behind it.
3.And lastly (I hope) in
if (str[i]!=32)
str[i] = str[i]+ offset;
why are we worried about str[i] not equaling 32?
I am sorry if this is a lot to be asking but I really truly want to understand this.
Also if you know of any fantastic reads for C please let me know because I am rather worried about the rest of this semester at this point.
EDIT:
Just wanted to say thank you very much to everyone who answered me. I am unfortunately not one of those people who immediately likes to continue asking more questions to acknowledge that you helped me. So for those of you who I didn’t directly thank or comment back, Thank you very much. I now have a much firmer grasp on some of the very elementary concepts I was nervous about only 30 mins ago.
char str[1024]because C does not have any concept of a string, only arrays ofcharterminated by NUL aka 0. It doesn’t have any concept of “stretchy” containers that you don’t explicitly implement yourself. So if you want to read a string of input without writing a lot of your own input handling you guess at some maximum length (in this case, 1024) and make space for that in advance.fgetsreads a string (and when you say “string” in C that means “array ofcharterminated by 0, as above). You pass instrwhich is the pointer to the start of the storage andsizeof(str)which is evaluated at compile time to be the number of bytes thatstrtakes up. It means1024in this case, with the advantage that it will track the change if you change thechar strline. That argument is preventingfgetsfrom writing beyond the allocated space ofstr. Remember, it’s not a stretchy string.str[i] != ' '