Sign in to follow this  
Followers 0
shani

Upper case to lower case and vice-versa in c#

4 posts in this topic

Hello,

I am new to this website and trying to learn c#. i want to know the mistake in my coding because when i run the program it doesnt show me anything as an answer, the example of how it should be 

the word entered = HeLLO

answer = hEllo

public static void Program3()
        {
           
            
 
            string strUserInput;
            System.Console.WriteLine("Please enter a word or a sentence:");
            strUserInput = System.Console.ReadLine();
 
 
            if (strUserInput == "a" || strUserInput == "b" || strUserInput == "c" || strUserInput == "d" || strUserInput == "e" || strUserInput == "f" || strUserInput == "g" || strUserInput == "h" || strUserInput == "i" || strUserInput == "j" || strUserInput == "k" || strUserInput == "l" || strUserInput == "m" || strUserInput == "n" || strUserInput == "o" || strUserInput == "p" || strUserInput == "q" || strUserInput == "r" || strUserInput == "s" || strUserInput == "t" || strUserInput == "u" || strUserInput == "v" || strUserInput == "w" || strUserInput == "x" || strUserInput == "y" || strUserInput == "z")
            {
                strUserInput.ToUpper();
                System.Console.WriteLine(strUserInput.ToLower());
            }
            if (strUserInput == "A" || strUserInput == "B" || strUserInput == "C" || strUserInput == "D" || strUserInput == "E" || strUserInput == "F" || strUserInput == "G" || strUserInput == "H" || strUserInput == "I" || strUserInput == "J" || strUserInput == "K" || strUserInput == "L" || strUserInput == "M" || strUserInput == "N" || strUserInput == "O" || strUserInput == "P" || strUserInput == "Q" || strUserInput == "R" || strUserInput == "S" || strUserInput == "T" || strUserInput == "U" || strUserInput == "V" || strUserInput == "W" || strUserInput == "X" || strUserInput == "Y" || strUserInput == "Z")
            {
                strUserInput.ToLower();
                System.Console.WriteLine(strUserInput.ToUpper());
            }
                       
 
            System.Console.ReadKey();
 
        }
 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

So you are flipping the case of the input text? What you are doing is pretty nonsensical.

You should instead enumerate every character of the input text. Here's an example.

string line = Console.ReadLine();
StringBuilder result = new StringBuilder();

for (int i = 0; i < line.Length; i++)
{
    if (char.IsUpper(line, i))
        result.Append(char.ToLower(line[i]));
    else if (char.IsLower(line, i))
        result.Append(char.ToUpper(line[i]));
    else
        result.Append(line[i]);
}

Console.WriteLine(result.ToString());

I haven't tested it, and wrote it right here in the browser, but it should be close to what you need.

Edited by Richard Robertson

Share this post


Link to post
Share on other sites

shani,

Welcome to the AutoIt forum. :)

I am new to this website and trying to learn c#

But if you have lots of c# questions then please find a c# site to ask them - we focus on AutoIt questions here. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

I haven't tested it, and wrote it right here in the browser, but it should be close to what you need.

Words of wisdom.


"Just be fred, all we gotta do, just be fred."  -Vocaliod

"That is a Hadouken. A KAMEHAMEHA would have taken him 13 days and 54 episodes to form." - Roden Hoxha

@tabhooked

Clock made of cursors ♣ Desktop Widgets ♣ Water Simulation

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0