Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all

How to convert Char?

i want this out put!!

Example:

First String: Kavin
Last Convert: kAVIN

How to convert by algorithm?

Please help me!!

thank you for post!!
Posted
Updated 9-Feb-11 2:13am
v2
Comments
Yusuf 9-Feb-11 11:07am    
When did you start using C#5

Very quick and very dirty:
C#
string k = "Kavin";
string k2 = "";
for(int i = 0; i < k.Length; i++)
{
  string tmp = k.Substring(i, 1);
  if (tmp.ToUpper() == tmp)
    k2 += tmp.ToLower();
  else
    k2 += tmp.ToUpper();
}
MessageBox.Show(k2); //displays "kAVIN"
 
Share this answer
 
Comments
Manas Bhardwaj 9-Feb-11 8:22am    
you beat me to it .:) +5
JF2015 9-Feb-11 8:24am    
But you showed me the "IsUpper" and "IsLower" - I hadn't seen them. 5ed you as well :)
Nuri Ismail 9-Feb-11 8:31am    
Good call! +5 :)
like this:

C#
public string ToggleCase(string PstrInput)
        {
            string LstrResult = string.Empty;
            char[] LarrInput = PstrInput.ToCharArray();
            foreach (char LobjChar in LarrInput)
            {
                if (char.IsLower(LobjChar))
                    LstrResult += LobjChar.ToString().ToUpper();
                else if (char.IsUpper(LobjChar))
                    LstrResult += LobjChar.ToString().ToLower();
                else
                    LstrResult += LobjChar.ToString();
            }
            return LstrResult;
        }
 
Share this answer
 
v2
Comments
Nuri Ismail 9-Feb-11 8:32am    
Good answer! +5 :)
And one alternate from me:

C#
public static void InvertCase(ref string strText, out string strInverted)
{
    char[] arrChars = strText.ToCharArray();
    for (int i = 0; i < arrChars.Length; i++)
    {
        if (char.IsLower(arrChars[i]))
            arrChars[i] = (char)((int)arrChars[i] - 32);
        else if (char.IsUpper(arrChars[i]))
            arrChars[i] = (char)((int)arrChars[i] + 32);
    }
    strInverted = new string(arrChars);
}


You can test the method:
C#
string strText = "Kavin", strInverted;
InvertCase(ref strText, out strInverted);
Console.WriteLine("Original: " + strText);
Console.WriteLine("Inverted: " + strInverted);
Console.ReadKey();


The output is:
Original: Kavin
Inverted: kAVIN


:)
 
Share this answer
 
Comments
JF2015 9-Feb-11 8:37am    
Good alternative. 5+
hi

thank you so much !!!

i love this code

But Do you have a new code ?

i don't want to use

ToUpper()

Do you have other way?

please??
 
Share this answer
 
Comments
JF2015 9-Feb-11 8:37am    
Then you should use Nuri Ismail's solution that works without the helping "ToUpper" Method.
Than you so much for All reply

:-O :-O :-O :-O :-O

:thumbsup::thumbsup::thumbsup::thumbsup::thumbsup:

:-D :-D
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900