1

Input String : "This is a String" Output " tTHIS IS A sTRING

I'm able to convert the upper case to lower case but somehow I'm not able to link them :(.

here is my code.. could someone please help me out here ?

static void Main()
{
    string lstr = "";
    string ustr = "";
    Console.WriteLine("Enter the string");
    string str = Console.ReadLine();

    char[] strchararr = str.ToCharArray();

    for (int i = 0; i < strchararr.Length; i++)
    {

        if (strchararr[i]>='a'&& strchararr[i]<='z')
        {
            ustr += char.ToUpper(strchararr[i]);

        }

        else if (strchararr[i] >= 'A' && strchararr[i] <= 'Z')
        {
            lstr+=char.ToLower(strchararr[i]);
        }
    }
}
Tom Blodget
  • 20,260
  • 3
  • 39
  • 72
Ash0214
  • 25
  • 1
  • 3

4 Answers4

5

Use linq

var converted = str.Select(x => char.IsUpper(x) ? char.ToLower(x) : char.ToUpper(x));
var result = new string(converted.ToArray);

Or a foreach approach

foreach (var c in str)
   if (char.IsUpper(c))
      result += char.ToLower(c);
   else 
      result += char.ToUpper(c);

or

foreach (var c in str)
    result += char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c);

or a for loop

for (int i = 0; i < str.Length; i++)
   if (char.IsUpper(str[i]))
      result += char.ToLower(str[i]);
   else 
      result += char.ToUpper(str[i]);

or

for (var i = 0; i < str.Length; i++)
     result += char.IsUpper(str[i]) ? char.ToLower(str[i]) : char.ToUpper(str[i]);

Or a ridiculously stupid (yet super efficient) unsafe pointer version (ASCII only)

fixed (char* cStr = str)
   for (var c = cStr; c < cStr + str.Length; c++)
     *c = (char)(*c >= 'A' && *c <= 'Z' ? *c + 32 : *c >= 'a' && *c <= 'z' ? *c - 32 : *c);
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
1

Your code has a few problems.

You are using two strings ustr and lstr. You should instead use one single string to store the result, and append the result of char.ToLower and char.ToUpper to that string.

You are also not handling anything else other than A-Z and a-z. You should add an else block to just append whatever the character is (no conversion needed).

    char[] strchararr = str.ToCharArray();

    for (int i = 0; i < strchararr.Length; i++)
    {

        if (strchararr[i]>='a'&& strchararr[i]<='z')
        {
            finalString += char.ToUpper(strchararr[i]);

        }

        else if (strchararr[i] >= 'A' && strchararr[i] <= 'Z')
        {
            finalString+=char.ToLower(strchararr[i]);
        } else {
            finalString += strchararr[i];
        }
    }

    Console.WriteLine(finalString);

If you want to handle letters from other alphabets (e.g. Cyrillic, Greek), you should use char.IsLower and char.IsUpper to check for case.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

By "link them" I suppose you mean how do you get a whole resulting string? In that case, no need to use both lstr and ustr variables, use just one.

For example: replace the line

        ustr += char.ToUpper(strchararr[i]);

with

        lstr += char.ToUpper(strchararr[i]);
Nikola Dim
  • 736
  • 7
  • 21
0

With char as numbers:
applicable when all the characters have CodePoints below 256.

StringBuilder sb = new StringBuilder(input.Length);
foreach (char c in input)
    sb.Append(!char.IsLetter(c) ? c : c < 97 ? (char)(c + 32) : (char)(c - 32));
Jimi
  • 29,621
  • 8
  • 43
  • 61