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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:41:12+00:00 2026-05-25T12:41:12+00:00

I am trying to print my string backwards in c and I just can’t

  • 0

I am trying to print my string backwards in c and I just can’t seem to get it working with whitespaces. I am aware that if there is any whitespace after the last returned character in the scanf function that it will terminate because there is no existing characters left to scan in from the string entered. Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLETTERS 20

void readStringBackwards();

main()
{
readStringBackwards();
}


void readStringBackwards()
{



printf("Please enter a string of up to 20 characters in length:\n ");
char str[20];
scanf("%s", str);
int i;
int stringlength = strlen(str);

if (stringlength > MAXLETTERS)
{
    printf("The string length is too long!");
}
else
{
    for( i = stringlength-1; i >= 0; i--){

        printf("%c ", str[i]);

    }

}



}

Essentially the program is supposed to accept up to 20 characters, and print the string backwards. I have been searching for information on scanf and how it works, but its been pretty hard to find a direct answer. Any tips are appreciated. Right now all I will get is the first word that I type in reverse. The other characters after an space are skipped and not stored within the array.

  • 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-25T12:41:13+00:00Added an answer on May 25, 2026 at 12:41 pm

    scanf("%s", str); will read up to a whitespace, even if its more than 20 chars. Blank, newline and tab are considered whitespace characters.

    Consider using a format specification like "%20[^\n]", that should be read up to 20 chars as long as they are not '\n'.

    edit: As Oli Charlesworth pointed out, the buffer would have to be at least length 21, not 20.

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

Sidebar

Related Questions

I am trying to make a function in C that will print a string
I'm trying to write a method that uses recursion to print the string formed
Trying to get hold of <spring:bind> tag. I am trying to print a String
Hey there, I'm trying to perform a backwards regular expression search on a string
I am trying to print a string in a way that's OS-neutral. For example,
In Ruby, trying to print out the individual elements of a String is giving
I'm trying to read in a multi line string then split it then print
I am trying to print a report that contains a bar graph using the
I have two string variables ticker and detail. I'm trying to print out the
Trying to print out some html forms but I get a parsing syntax error.

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.