locked
getchar() method and char convertion

    Question

  • Hi Guys,

    I have two question:

    - how do i use getchar() to collect characters in a string?
    - how to convert upper case chars to lower and vice sersa?

    Thanks in Advance.
    Sunday, June 07, 2009 2:53 PM

Answers

  • There's several functions called getchar, so which one you're talking about? but to get the characters of a string you can use
    myString[0] to access the first character of myString string.
    for you second question here's an example
                string myString = "tesT strinG";
    
    
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
    
    
                foreach (char chr in myString)
                {
    
                    if (char.IsUpper(chr))
                    {
                        sb.Append(char.ToLower(chr));
                    }
                    else 
                    {
    
                        sb.Append(char.ToUpper(chr));
    
                    }
                }
    
                MessageBox.Show(sb.ToString());
                

    My Personal Blog
    Sunday, June 07, 2009 3:15 PM
  • I'll guess you are talking about the C/C++ getchar() function.  The managed equivalent is Console.Read().  Use Char.ToLower() and ToUpper() or String.ToLower/Upper.

    Hans Passant.
    • Marked as answer by jonanini Monday, June 08, 2009 9:28 AM
    Sunday, June 07, 2009 6:20 PM
    Moderator
  • hi Sr,
    i am in c# and i meant for example:

    string str="maSoNneTTe";

    i'd like to convert lower cases to upper case or upper cases to lower case.
    and count how many of each type was found in the string: -upper cases =4;
                                                                                        -lower cases=6;

    • Marked as answer by jonanini Monday, June 08, 2009 9:30 AM
    Monday, June 08, 2009 7:14 AM
  • hi Sr,
    i am in c# and i meant for example:

    string str="maSoNneTTe";

    i'd like to convert lower cases to upper case or upper cases to lower case.
    and count how many of each type was found in the string: -upper cases =4;
                                                                                        -lower cases=6;


    jonanini,

    In my idea should you first mark the by nobugz proposed correct answer Rashad marked gave you as Answer, and then simple ask a new question.

    This has nothing to do with converting a string to an array of char

    • Marked as answer by jonanini Monday, June 08, 2009 9:30 AM
    Monday, June 08, 2009 8:00 AM

All replies

  • There's several functions called getchar, so which one you're talking about? but to get the characters of a string you can use
    myString[0] to access the first character of myString string.
    for you second question here's an example
                string myString = "tesT strinG";
    
    
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
    
    
                foreach (char chr in myString)
                {
    
                    if (char.IsUpper(chr))
                    {
                        sb.Append(char.ToLower(chr));
                    }
                    else 
                    {
    
                        sb.Append(char.ToUpper(chr));
    
                    }
                }
    
                MessageBox.Show(sb.ToString());
                

    My Personal Blog
    Sunday, June 07, 2009 3:15 PM
  • I'll guess you are talking about the C/C++ getchar() function.  The managed equivalent is Console.Read().  Use Char.ToLower() and ToUpper() or String.ToLower/Upper.

    Hans Passant.
    • Marked as answer by jonanini Monday, June 08, 2009 9:28 AM
    Sunday, June 07, 2009 6:20 PM
    Moderator
  • hi Sr,
    i am in c# and i meant for example:

    string str="maSoNneTTe";

    i'd like to convert lower cases to upper case or upper cases to lower case.
    and count how many of each type was found in the string: -upper cases =4;
                                                                                        -lower cases=6;

    • Marked as answer by jonanini Monday, June 08, 2009 9:30 AM
    Monday, June 08, 2009 7:14 AM
  • What do you want to do with none language characters which can't be upper or lower case?
    for example "Hi Joe-" has two upper, three lower case and two characters which can't be upper or lower?


    My Personal Blog
    • Marked as answer by jonanini Monday, June 08, 2009 9:29 AM
    • Unmarked as answer by jonanini Monday, June 08, 2009 9:30 AM
    Monday, June 08, 2009 7:22 AM
  • hi Sr,
    i am in c# and i meant for example:

    string str="maSoNneTTe";

    i'd like to convert lower cases to upper case or upper cases to lower case.
    and count how many of each type was found in the string: -upper cases =4;
                                                                                        -lower cases=6;


    jonanini,

    In my idea should you first mark the by nobugz proposed correct answer Rashad marked gave you as Answer, and then simple ask a new question.

    This has nothing to do with converting a string to an array of char

    • Marked as answer by jonanini Monday, June 08, 2009 9:30 AM
    Monday, June 08, 2009 8:00 AM