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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:52:47+00:00 2026-05-27T12:52:47+00:00

My program’s goal is to receive from the standard input a text, and a

  • 0

My program’s goal is to receive from the standard input a text, and a ‘number’, and to return to the screen the text after shifting of the letters ‘number’ times (for shift 3, ‘a’ becomes ‘d’, ‘e’ becomes ‘g’). It should only shift the lower case letter and should be cyclic (letter ‘y’ in a shift of 3 should become ‘a’ again).

I have some bugs, though. If I shift ‘t’ 11 times, it’ll come to ‘e’, but if I shift ‘t’ 12 times, I get space (” “). Why? Here’s my code:

  3 int main(int argc, char *argv[]) {
  4    if (argc != 2) {
  5       printf ("Wrong input\n");
  6       return 1;
  7    }
  8    int shift = atoi (argv[1]);
  9    int c;
 10    while ((c = getchar()) != EOF) {
 11       if (c >= 'a' && c <= 'z') {
 12          char newch = c + shift;
 13          if (newch > 'z') {
 14             newch = 'a' + (newch - 'z' - 1);
 15          }
 16          if (newch < 'a') {
 17             newch = 'z' - ('a' - newch - 1);
 18          }
 19          putchar (newch);
 20       }
 21       else
 22          putchar(c);
 23    }
 24    return 0;
 25 }

Also, when compiling I receive those warnings:

shift_chars.c: In function `main':
shift_chars.c:8: warning: implicit declaration of function `atoi'
shift_chars.c:8: warning: ISO C90 forbids mixed declarations and code

What do those mean?

  • 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-05-27T12:52:47+00:00Added an answer on May 27, 2026 at 12:52 pm

    The first warning means you did not include <stdlib.h>, which is where atoi() is declared.

    The second warning means that you declared variables (shift etc) after you had some executable code. C++ allows that; C99 allows that; ISO C 90 (ANSI C 89) did not allow that. In older C, all variables in a block had to be defined before any executable statements.

    Also, if you are shifting 3 and translate ‘y’ to ‘a’ you have a bug — it should be ‘b’ (‘z’, ‘a’, ‘b’).

    One of your problems is that the code for ‘z’ is 122, and adding 11 to 122 wraps you to a negative value if your characters (char type) are signed; you do not have this problem if char is unsigned. You’d probably do best to use an int instead of char while computing the new character value.

    You might find it easier to manage if you calculate:

    int newch = ((c - 'a') + shift) % 26 + 'a';
    

    The c - 'a' gives you an index into the alphabet: a = 0, b = 1, … z = 25. Add the shift; take the result modulo 26 to get the offset into the alphabet again; then add the result to 'a' to get the correct letter. Since c - 'a' generates an integer (because all char values are promoted to int), this avoid any overflows in char.

    Indeed, you could avoid newch altogether and simply compute:

    a = (c - 'a' + shift) % 26 + 'a';
    

    You can then omit the putchar() at line 19, and drop the else, leaving the putchar(c); to output both transformed and untransformed characters.

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

Sidebar

Related Questions

My program collects the number of bottles collected by four rooms. When the user
My program has 3 text fields, Title, Website, and PictureURL. When I click the
Example program: list links This example program demonstrates how to fetch a page from
My program requires the result from inside an loop to be displayed or outputted
Program followed by output. Someone please explain to me why 10,000,000 milliseconds from Jan
// Program to print simple text on a Printer import javax.swing.*; import java.awt.*; import
// Program to print simple text on a Printer import javax.swing.*; import java.awt.*; import
Most program languages have some kind of exception handling; some languages have return codes,
Strange program hang, what does this mean in debug? After attaching windbg I found
Program A, is a c program that endlessly, receives input in stdin, process it

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.