Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, I must create a console application that lets a user enter any sentence and then be asked if he wants to replace a word in that sentence for example "Aliens are very real and looks scary." Ask which word does he want to replace like "very" with "not". Then ask user with yes/no question if he wants to replace another word. If yes then replace word like "scary" with "funny" if no display "Aliens are not real and looks scary." Keep on asking user if he wants to replace more words until user says "no".

My code looks like this so far...I need help with the yes/no question and then prompting user with the question again and using that in the replaced sentence ...please help me

C#
static void Main(string[] args)
        {
            //Enter sentence
            Console.WriteLine("Enter your sentence : \n\n");
            string sSentence = Console.ReadLine();
            Console.WriteLine("\n");


            //Words to be replaced
            Console.WriteLine("Which word do you want to replace? \n");
            string sFirstWord = Console.ReadLine();
            Console.WriteLine("\n");
            Console.WriteLine("Enter the word you want to replace it with: \n");
            string sFirstWordReplace = Console.ReadLine();
            Console.WriteLine("\n");

            //
            //Not sure what to do here with yes/no
            //and redisplay questions if yes
            //

            //Call yes/no question
            AnotherOne();

            //If yes ask second word to be replaced
            Console.WriteLine("Which word do you want to replace? \n");
            string sSecondWord = Console.ReadLine();
            Console.WriteLine("\n");
            Console.WriteLine("Enter the word you want to replace it with: \n");
            string sSecondWordReplace = Console.ReadLine();
            Console.WriteLine("\n");

            //Replace the words
            string sFirst = sSentence.Replace(sFirstWord, sFirstWordReplace);
            Console.WriteLine(sFirst);

            //Exit the program
            Console.WriteLine("\n");
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }

        static bool AnotherOne()
        {
            while (true)
            {
                Console.WriteLine("Do you want to replace another word? \n");
                String r = (Console.ReadLine() ?? "").ToLower();
                if (r == "y")
                    return true;
                if (r == "n")
                    return false;
            }
        }
Posted

assuming that your code do what it suppose to do

It would looks like :

static void Main(string[] args)
 {
    //Enter sentence

     while ( wantToReplace() ){
          //Words to be replaced
          //Replace the words
      }

     //Exit the program
}
 
static bool wantToReplace() {

    Console.WriteLine("Do you want to replace another word? \n");
                String r = (Console.ReadLine() ?? "").ToLower();
                if (r == "y")
                    return true;
                if (r == "n")
                    return false;
            
 }



note : the comment line indicates that code section that you have
 
Share this answer
 
Comments
Spekskieter 4-Aug-13 13:45pm    
This helped a lot, just need to tweak it a bit but it points me in the right direction, thanks!!!
sorawit amorn 4-Aug-13 13:52pm    
no problem
ridoy 4-Aug-13 13:52pm    
+5
You can use String.Replace Method (String, String)[^] to replace changes string data. It changes all occurrences of one substring into another substring.
 
Share this answer
 
Comments
Spekskieter 4-Aug-13 13:45pm    
Don't quite understand what to do?

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