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 8580945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:56:03+00:00 2026-06-11T20:56:03+00:00

I have some embarrassingly elementary questions that I cannot seem to find a an

  • 0

I have some embarrassingly elementary questions that I cannot seem to find a an answer for that I am able to process. I had incredible luck the last time I asked you folks for some help, so I am hoping for a repeat. I have no prior experience in C/C++ of any kind and this class assumes at least 1 semester, so its kicking my but a bit.

This is the code I am given by my professor:

#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) {

// add your code here
}

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:
}
}

This is what I am suppose to perform:

  • Convert the code to C++. Replace all printf, scanf, and fgets statements.
  • Add codes to the “decrypt” method to decipher the encrypted text.
  • Change the code to use pointer operations instead of array operations to encrypt and decrypt messages.
  • In the main method, call the “decrypt” method to decipher the encrypted text (str).

I am not asking for answers directly to the assignment but I need help undertanding the errors I am making and what I need to be doing.

this is my attempt at the first part of the homework:

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

void encrypt(int offset, char *str) {

int i,l;

l=strlen(str);

cout << "\nUnencrypted str = \n%s\n", str;

for(i=0;i<l;i++)
    if (str[i]!=32)
        str[i] = str[i]+ offset;

cout << "\nEncrypted str = \n%s \nlength = %d\n", str, l;
}

void decrypt(int offset, char *str) {

// add your code here
}

int main(void) {

char str[1024];

cout << "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:
}
}

problem is this is what I am getting out and I cant figure out what needs to be done differently.

Please enter a line of text, max %d characters
hellow world

Unencrypted str =
%s

Encrypted str =
%s
length = %d

At this point ill take anything, I have already spent a couple hours googling and trying things without really getting anywhere and I know this is something that should take no longer than an hour.

  • 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-11T20:56:05+00:00Added an answer on June 11, 2026 at 8:56 pm

    looking at the “Change the code to use pointer operations instead of array operations to encrypt and decrypt messages”, let’s see if i can explain this one. (i will focus on the pointer part). i will rewrite the encrypt function a couple of times to hopefully make it clear

    first, we convert the for into a while loop

    void encrypt(int offset, char *str) 
    {
        int i,l;
    
        l=strlen(str);
    
        printf("\nUnencrypted str = \n%s\n", str);
    
        i = 0;
        while(i < l)
        {
            if (str[i]!=32)  
                str[i] = str[i]+ offset;
            ++i;
        }
    
        printf("\nEncrypted str = \n%s \nlength = %d\n", str, l);
    }
    

    now, we try to get rid of the strlen call. strlen counts all characters between the start of a C string and the first appereance of a null character. A null character can be written as '\0' but in C or C++ that is pretty much the same as writing just 0. so similar to comparing str[i] != 32, we can write str[i] != 0 if we want to loop as long as the character at position i of string str is not 0.

    void encrypt(int offset, char *str) 
    {
        int i;
    
        printf("\nUnencrypted str = \n%s\n", str);
    
        i = 0;
        while(str[i] != 0)
        {
            if (str[i]!=32)  
                str[i] = str[i]+ offset;
            ++i;
        }
    
        printf("\nEncrypted str = \n%s \nlength = %d\n", str, l);
    }
    

    str[i] is equivalent to the following *(str + i) it is just written differently.
    so we can rewrite again

    void encrypt(int offset, char *str) 
    {
        int i;
    
        printf("\nUnencrypted str = \n%s\n", str);
    
        i = 0;
        while(*(str + i) != 0)
        {
            if (*(str + i)!=32)  
                *(str + i) = *(str + i) + offset;
            ++i;
        }
    
        printf("\nEncrypted str = \n%s \nlength = %d\n", str, l);
    }
    

    now we already have solution only using pointers, but we can make it more pretty by understanding pointers better.

    let’s rewrite again using another variable

    void encrypt(int offset, char *str) 
    {
        int i;
    
        printf("\nUnencrypted str = \n%s\n", str);
    
        i = 0;
        char *ptr = (str + i);
        while(*ptr != 0)
        {
            if (*ptr!=32)  
                *ptr = *ptr + offset;
            ++i;
            ptr = (str + i);
        }
    
        printf("\nEncrypted str = \n%s \nlength = %d\n", str, l);
    }
    

    so all i did here is, i substituted (s + i) by the new variable ptr.
    and in the final step, we remove the i, since it is just a helper variable the tells us how many places we have to jump from the start address. but since it increases by just one in every iteration, we just move the pointer directly.

    void encrypt(int offset, char *str) 
    {
        printf("\nUnencrypted str = \n%s\n", str);
    
        char *ptr = str;
        while(*ptr) // the same as *ptr != 0
        {
            if (*ptr!=32)  
                *ptr = *ptr + offset;
            ++ptr;
        }
    
        printf("\nEncrypted str = \n%s \nlength = %d\n", str, l);
    }
    

    and then replace your printf calls by std::cout calls as suggested by Seth Carnegie.

    for the decryption: just try to see what is happening in encrypt: when encrypt 'sees' a chracter that is not equal to 32, it addsoffset` to that character and assigns it back. so try to think about what you have to do, to revert this operation (which has nothing to do with C at all)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have some problems with a Query seem IN dosen't work with Group_concat, that
I have some arbitrary pixel data that I want to save as a PNG.
I have some text lines like that : vt_wildshade2^508^508 vt_ailleurs2^1188^1188 ... vt_high2^13652^13652 Is it
I have some code that will change the background color of a specific label
I have some code here that uses bitsets to store many 1 bit values
I have a simple request that I have spent much time on (embarrassingly).. I
I have some values that are stored with core data, and I have opened
I had some problems with vertical spacing in Firefox and IE for an embarrassingly
Have some dates in my local Oracle 11g database that are in this format:
I have some embarrassingly-parallelizable work in a .NET 3.5 console app and I want

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.