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

  • Home
  • SEARCH
  • 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 7412537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:33:21+00:00 2026-05-29T06:33:21+00:00

My head is getting twisted trying to figure this out. The assignment is to

  • 0

My head is getting twisted trying to figure this out.

The assignment is to get a line of text from the user and then print it out in reverse including the Hex code for each character.

I thought it would have been simple, and probably is once I understand the usage.

My thoughts were, I would set up my variable using char stg_array[80] whuch would give me a max size of 80 char.

I would then do a

while( (stg_array[i] = getch() ) != '\n' )
{
    i++;
}

Then I would use strlen() to determine the actual length (I realize I already have it, just want to use strlen() for practice) and drop into a for loop that would countdown from the strlen(stg_array)-1 and

printf("Array[%d] = %s Hex Value = %X", i, stg_array[i], i );

I understand the buffer overflow bug I could have if the user types more than 80 chars. Not worried about that right now.

What I’m getting is the strlen() is returning the length to the first space in the text.

If I skip the user input and just make char stg_array[80]={"The quick brown fox."} with a fixed for loop in the middle, say 10, the program crashes.

I’m thinking I’m out to lunch about how C string arrays function.

  • 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-29T06:33:21+00:00Added an answer on May 29, 2026 at 6:33 am

    Hope the following helps!

    #include <stdio.h>
    
    #define MAX 80
    
    int main(void)
    {
        char stg_array[MAX];
        int i = 0, j;
        int len;
    
        printf("Enter String : ");
        while((i < MAX-1) && ((stg_array[i] = getchar()) != '\n')) {
            i++;
        }
        stg_array[i] = '\0';
    
        for (j=i ; j>=0 ; j--) {
            printf("stg_array[%d] = %c / %x \n", j, stg_array[j], stg_array[j]);
        }
    
        return 0;
    }
    

    Output:

    Enter String : this is a test string
    stg_array[21] =  / 0 
    stg_array[20] = g / 67 
    stg_array[19] = n / 6e 
    stg_array[18] = i / 69 
    stg_array[17] = r / 72 
    stg_array[16] = t / 74 
    stg_array[15] = s / 73 
    stg_array[14] =   / 20 
    stg_array[13] = t / 74 
    stg_array[12] = s / 73 
    stg_array[11] = e / 65 
    stg_array[10] = t / 74 
    stg_array[9] =   / 20 
    stg_array[8] = a / 61 
    stg_array[7] =   / 20 
    stg_array[6] = s / 73 
    stg_array[5] = i / 69 
    stg_array[4] =   / 20 
    stg_array[3] = s / 73 
    stg_array[2] = i / 69 
    stg_array[1] = h / 68 
    stg_array[0] = t / 74 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to wrap my head around getting user input from a joystick/mouse, which
My head is getting bad trying to find a solution for this assignment because
I'm having problems trying to get my head around getting a collection of types
I am getting my head around PHPUnit, and trying to build a test case
I'm having a hard time getting my head around this, and Google just isn't
I'm having hard time getting my head wrapped around this one - and it
I am still getting my head round regex and learning little by little. This
I'm having a hard time getting my head around a query im trying to
I'm trying to wrap my head around getting nodes that share the same taxonomy
hi again have bent my head on getting this to work for 3 days

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.