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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:25:45+00:00 2026-05-25T03:25:45+00:00

Trying to reverse a string but getting error in Aggregate function private string Reverse(string

  • 0

Trying to reverse a string but getting error in Aggregate function

private string Reverse(string strValue)
        {

            char[] chArray = strValue.ToCharArray();
            var reverse = chArray.Reverse();

            var res = reverse.Aggregate((a,b)=>a+b);

            return res.ToString();
        }

Cannot implicitly convert type ‘int’ to ‘char’. An explicit conversion exists (are you missing a cast?)
So what is the mistake?

  • 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-25T03:25:46+00:00Added an answer on May 25, 2026 at 3:25 am

    Exactly as it says: the result of a + b is an int, but you want it to be a char. To get it to compile you can just use:

    var res = reverse.Aggregate((a,b)=>(char) (a+b));
    

    … but I don’t think that will do what you want it to.

    I suggest you don’t use LINQ for this to start with:

    private string Reverse(string strValue)
    {
        char[] chArray = strValue.ToCharArray();
        Array.Reverse(chArray);
        return new string(chArray);
    }
    

    Note that this doesn’t work properly with things like combining characters, surrogate pairs etc.

    If you really wanted to use LINQ, you could do it with:

    private string Reverse(string strValue)
    {
        return new string(strValue.Reverse().ToArray());
    }
    

    There’s no need to call Aggregate. If you really want to make it slow and use string concatenation with Aggregate (it’ll be O(n2)) you can do it like this:

    private string Reverse(string strValue)
    {
        return strValue.Reverse().Aggregate("", (str, c) => str + c);
    }
    

    Here’s we’ve provided a string seed value, so the parameter str will be a string and + will be string concatenation, not character addition.

    You can make it more efficient using StringBuilder:

    private string Reverse(string strValue)
    {
        return strValue.Reverse()
                       .Aggregate(new StringBuilder(), (sb, c) => sb.Append(c))
                       .ToString();
    }
    

    … but I’m still not sure it’s worth it…

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

Sidebar

Related Questions

I'm trying to reverse an input string var oneway = document.getElementById('input_field').value(); var backway =
public String reverse(String sentence){ String reverse = ; char [] s = sentence.toCharArray(); int
I was trying to reverse a number in PL/SQL. It's working fine, but when
I am trying to reverse a character string in C Here is what I
I am trying to write code to reverse a string in place (I'm just
Okay, so I'm trying to reverse a C style string in C++ , and
I'm currently trying to reverse geocode a series of lat/long co-ordinates using the Virtual
I'm trying to reverse the order of bits in C (homework question, subject: bitwise
I'm trying to reverse engineer how facebook handles their notifications, where when you get
My URLconf contains this pattern: url(r'^accounts/logout/$','django.contrib.auth.views.logout', name=logout), And I've trying to reverse that in

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.