Home > Software engineering >  Program to reverse string and make it uppercase?
Program to reverse string and make it uppercase?

Time:09-06

I wrote this program like the title says but it says system.char[] for some reason.

using System;
public class q2
{
    public static void upperNreverse(string inp)
    {
        char[] inpChar = inp.ToCharArray();
        Array.Reverse(inpChar);
        string inpString = Convert.ToString(inpChar);
        string finalString = inpString.ToUpper();
        Console.WriteLine(finalString);
    }
    public static void Main()
    {
        Console.WriteLine("Please enter the string to convert to uppercase");
        string inp = Console.ReadLine();
        upperNreverse(inp);
        
    }
}

CodePudding user response:

Convert.ToString() is not suitable for converting a char array to a string. Use the constructor new string(char[])

Change

string inpString = Convert.ToString(inpChar);

to

string inpString = new string(inpChar);

CodePudding user response:

Use this code,

using System;
public class q2
{
    public static void upperNreverse(string inp)
    {
        char[] inpChar = inp.ToCharArray();
        Array.Reverse(inpChar);
        String inpString = new String(inpChar);
        string finalString = inpString.ToUpper();
        Console.WriteLine(finalString);
    }
    public static void Main()
    {
        Console.WriteLine("Please enter the string to convert to uppercase");
        string inp = Console.ReadLine();
        upperNreverse(inp);

    }
}

This is string inpString = Convert.ToString(inpChar); not the correct format for converting the char[] to string. Use these code

String inpString = new String(inpChar);
  • Related