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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:13:33+00:00 2026-05-25T01:13:33+00:00

I got a problem about how to reverse a string containing this ‘abcd汉字efg’. str_to_reverse

  • 0

I got a problem about how to reverse a string containing this ‘abcd汉字efg’.

str_to_reverse = "abcd汉字efg"; /* those non-ASCII chars are Chinese characters, each of them takes 2 bytes */

after reversion, it should be:

str_toreverse = "gfe字汉dcba";

I thought, to reverse the string, I gotta identify those non-ASCII chars, because I think that simply reversing every byte won’t get the right answer.

How can I do it?

PS:
I wrote this program under Ubuntu, 32-bit.
then I printed every byte:

for(i = 0; i < strlen(s); i++)
    printf("%c", s[i]);

I got some gibberish text instead of “汉字”.

  • 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-25T01:13:34+00:00Added an answer on May 25, 2026 at 1:13 am

    Pure C89 answer:

    #include <stdlib.h>
    #include <stdio.h>
    #include <locale.h>
    #include <string.h>
    
    int main()
    {
        char const* str;
        size_t slen;
        char* rev;
    
        setlocale(LC_ALL, "");
        str = "abcd汉字efg";
        printf("%s\n", str);
        slen = strlen(str);
        rev = malloc(slen+1)+slen;
        *--rev = '\0';
        while (*str != '\0') {
            int clen, i;
            clen = mblen(str, slen);
            if (clen == -1) {
                fprintf(stderr, "Bad encoding\n");
                return EXIT_FAILURE;
            }
            for (i = 0; i < clen; ++i) {
                *--rev = str[clen-1-i];
            }
            str += clen;
        }
        printf("%s\n", rev);
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a problem, it's about converting from List to String to edit it
I've got a problem about onbeforeunload recently that I need to pop up a
I have got a strange problem about in_array recently which I cannot understand. e.g.
I have got a performance problem about TextField.htmlText +=msg .And I know that TextField.appendText(msg)
I've hit an annoying problem in LaTeX. I've got a tex file of about
I got a problem like this (this is html/css menu): Eshop | Another eshop
I've got a problem about RegOpenKeyEx, the code: #include <tchar.h> #include <stdio.h> #include <windows.h>
Got a little problem i asked about it before but maybe i didnt ask
Reading about how Google solves the translation problem got me thinking. Would it be
I've got a problem that I'm rather confused about. I have the following lines

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.