Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8540381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:35:06+00:00 2026-06-11T11:35:06+00:00

First I would like to thank everyone for everything they have ever done for

  • 0

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:

  1. Why write char str[1024]

  2. 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:35:07+00:00Added an answer on June 11, 2026 at 11:35 am
    1. You use char str[1024] because C does not have any concept of a string, only arrays of char terminated 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.
    2. fgets reads a string (and when you say “string” in C that means “array of char terminated by 0, as above). You pass in str which is the pointer to the start of the storage and sizeof(str) which is evaluated at compile time to be the number of bytes that str takes up. It means 1024 in this case, with the advantage that it will track the change if you change the char str line. That argument is preventing fgets from writing beyond the allocated space of str. Remember, it’s not a stretchy string.
    3. ASCII 32 is space. It would have been clearer to write str[i] != ' '
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right first of I would like to thank everyone for helping me out on
First I would like to say that I have only worked with java for
first of all I would like to thank you for the many good posts
It's my first post on here, and I'd just like to thank everyone so
First off I would like to say that I think that my question may
First I would like to say that I am new to this site, never
Please take a look at this screenshot first: I would like to move the
I would first like to admit that I am an extremely novice developer, so
First off I would like to say that this is my first post and
I'm using : log_file = AWS::S3::Bucket.logs_for('downloads.idance.net').first I would then like to delete each file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.