String program #15 (First
reverse the string than make the even position character to upper case)
Enter String: imagination
O/p: -
Reverse String: noitanigami
Reverse String: NoItAnIgAm
The program for the string is written
in C# programming language and will accept a string as input. The logic is to make
the even position character to upper case.
Let’s find out a simple and easy way
to code the program.
Practical Implementation:
using System;
namespace patternProblem.String
{
class String15 //First reverse the string than make the even position characters
to upper case
{
public static void Main()
{
Console.Write("Enter String: ");
string str = Console.ReadLine();
String15 obj = new String15();
//this line
make the string in reverse direction
string reverseStr = obj.strReverse(str);
Console.WriteLine("Reverse String: {0}", reverseStr);
char[] ch = new char[reverseStr.Length - 1];
for (int i = 0; i
< reverseStr.Length - 1; i++)
{
if (i % 2 == 0)
{
//Even position
ch[i] = Char.ToUpper(reverseStr[i]);
}
else
{
//Odd position
ch[i] = reverseStr[i];
}
}
string strUpper = new string(ch);
Console.WriteLine("Reverse String: {0}", strUpper);
}
public string
strReverse(string inputStr)
{
string str = inputStr;
char[] ch = str.ToCharArray();
//In-built
menthod to reverse the string
Array.Reverse(ch);
return new string(ch);
//User-defined
logic to reverse the string
//for (int i =
str.Length - 1; i >= 0; i--)
//{
// rev += str[i].ToString();
//}
//return rev;
}
}
}
Output:
Based on
the input, program will first reverse the string and then make the even position
character to upper case. The output is shown below:
Enter String: imagination
Reverse String: noitanigami
Reverse String: NoItAnIgAm
Press any key to continue . . .
For any
query, comment us below.
Skip to
Main Table String based program
Keep learning and sharing...
No comments:
Post a Comment