Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

String doesnt seem to return

The string in NewString doesnt seem to return to UserType() function. Is it wrong to declare that variable there? I want to return values to UserType so I can use it in another function like the Vowel function.


using System;

namespace prob1{
    class Pogram{
        static void Main(string[] args){
            UserType();
        }

        static void Menu(){
            Console.WriteLine("\nChoice of Operation:");
            Console.WriteLine("1. Enter new/another string");
            Console.WriteLine("2.  Count vowels in string and display result");
            Console.WriteLine("3. Count consonants in string and display result");
            Console.WriteLine("4. Convert string to uppercase letters and display");
            Console.WriteLine("5. Convert string to lowercase letters and display");
            Console.WriteLine("6. Count number of words in the string");
            Console.WriteLine("7. Exit Program");
        }

        static void UserType(){
            string mainString = System.String.Empty;
            Menu();
            int menuChoice;
            menuChoice = Int32.Parse(Console.ReadLine());
            
                 switch (menuChoice)
            {
                case 1:
                    NewString(mainString);
                    
                    UserType();
                    break;
                case 2:
                    Vowel(mainString);
                    UserType();
                    break;
                default:
                    break;
            }
            
        }

        static string NewString(string mainString){
            Console.WriteLine("Enter a new string: ");
            mainString = Console.ReadLine().ToLower();
      
            return mainString;
        }

        static void Vowel(string mainString){
            int total = 0;
            var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' };
            
    
            for (int finder = 0; finder < mainString.Length; finder++)
            {
                if (vowels.Contains(mainString[finder]))
                {
                total++;
                }
            }

            Console.WriteLine("total: " + total);
            
            Console.ReadKey(true);

        }
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You call NewString(mainString) method but didn’t assign the returned value from the method back to mainString. So in UserType method, the mainString value won’t be updated based on the result from NewString(mainString).

case 1:
    NewString(mainString);
    Console.WriteLine("test menu string: "+ mainString);
    UserType();
    break;

You need is to assign the result NewString(mainString) back to mainString.

mainString = NewString(mainString);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading