replace vowels using if else statement

kurumi

New member
Joined
Sep 15, 2013
Messages
1
Programming Experience
Beginner
How does one use an if else statement to replace the vowels in a string?
given a string s = "instruction"

how can one replace:
i with e
u with a
o with i

using an if else statement
here is my attempt:
C#:
[FONT=Consolas] [COLOR=#444444][/COLOR][COLOR=#009695]class[/COLOR][COLOR=#444444] [/COLOR][COLOR=#3364a4]MainClass[/COLOR]
[COLOR=#444444]    [/COLOR][COLOR=#444444]{[/COLOR]
[COLOR=#444444]        [/COLOR][COLOR=#009695]public[/COLOR][COLOR=#444444] [/COLOR][COLOR=#009695]static[/COLOR][COLOR=#444444] [/COLOR][COLOR=#009695]void[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]Main[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]([/COLOR][COLOR=#009695]string[/COLOR][COLOR=#444444][][/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]args[/COLOR][COLOR=#444444])[/COLOR]
[COLOR=#444444]        [/COLOR][COLOR=#444444]{[/COLOR]
[COLOR=#444444]            [/COLOR][COLOR=#009695]string[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]s = "instructor's Manual"[/COLOR][COLOR=#444444];[/COLOR][/FONT]
[FONT=Consolas] [COLOR=#444444][/COLOR][COLOR=#009695]      for[/COLOR][COLOR=#444444]([/COLOR][COLOR=#009695]int[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]i[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]=[/COLOR][COLOR=#444444] [/COLOR][COLOR=#f57d00]0[/COLOR][COLOR=#444444];[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]i[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]<[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]s[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]Length[/COLOR][COLOR=#444444];[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]i[/COLOR][COLOR=#444444]++[/COLOR][COLOR=#444444])[/COLOR]
[COLOR=#444444]            [/COLOR][COLOR=#444444]{[/COLOR][/FONT][FONT=Consolas] [COLOR=#444444][/COLOR][COLOR=#009695]if[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]([/COLOR][COLOR=#444444]s[/COLOR][COLOR=#444444][[/COLOR][COLOR=#444444]i[/COLOR][COLOR=#444444]][/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]==[/COLOR][COLOR=#444444] [/COLOR][COLOR=#f57d00]'[/COLOR][COLOR=#f57d00]i[/COLOR][COLOR=#f57d00]'[/COLOR][COLOR=#444444])[/COLOR][COLOR=#444444] [/COLOR]
[COLOR=#444444]                    [/COLOR][COLOR=#444444]{[/COLOR]
[COLOR=#444444]                        [/COLOR][COLOR=#3364a4]Console[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]Write[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]([/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#f57d00]e[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#444444])[/COLOR][COLOR=#444444];[/COLOR]
[COLOR=#444444]                    [/COLOR][COLOR=#444444]}[/COLOR][COLOR=#444444] 
[/COLOR][COLOR=#009695]              else[/COLOR][COLOR=#444444] if([/COLOR][/FONT][FONT=Consolas][COLOR=#444444][/COLOR][COLOR=#444444]s[/COLOR][COLOR=#444444][[/COLOR][COLOR=#444444]i[/COLOR][COLOR=#444444]][/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]==[/COLOR][COLOR=#444444] [/COLOR][COLOR=#f57d00]'[/COLOR][COLOR=#f57d00]u[/COLOR][COLOR=#f57d00]'[/COLOR][/FONT][FONT=Consolas][COLOR=#444444])
                   {
[/COLOR][/FONT]
[FONT=Consolas][COLOR=#3364a4]                        Console[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]Write[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]([/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#f57d00]a[/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#444444])[/COLOR][COLOR=#444444];[/COLOR][/FONT][FONT=Consolas][COLOR=#444444]
                    }
              [/COLOR][/FONT][FONT=Consolas][COLOR=#009695]else[/COLOR][COLOR=#444444] if([/COLOR][/FONT][FONT=Consolas][COLOR=#444444][/COLOR][COLOR=#444444]s[/COLOR][COLOR=#444444][[/COLOR][COLOR=#444444]i[/COLOR][COLOR=#444444]][/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]==[/COLOR][COLOR=#444444] [/COLOR][COLOR=#f57d00]'o[/COLOR][COLOR=#f57d00][/COLOR][COLOR=#f57d00]'[/COLOR][/FONT][FONT=Consolas][COLOR=#444444])
                   {
[/COLOR][/FONT]
[FONT=Consolas][COLOR=#3364a4]                        Console[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]Write[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]([/COLOR][COLOR=#f57d00]"i[/COLOR][COLOR=#f57d00][/COLOR][COLOR=#f57d00]"[/COLOR][COLOR=#444444])[/COLOR][COLOR=#444444];[/COLOR][/FONT][FONT=Consolas][COLOR=#444444]
                    }[/COLOR][/FONT][FONT=Consolas]
            [/FONT]
[FONT=Consolas][COLOR=#444444][/COLOR][/FONT][FONT=Consolas][COLOR=#009695]                else[/COLOR][COLOR=#444444] [/COLOR][/FONT][FONT=Consolas][COLOR=#444444]   
                    [/COLOR][COLOR=#444444]{[/COLOR]
[COLOR=#444444]                        [/COLOR][COLOR=#999988][I][/I][/COLOR]
[COLOR=#444444]                        [/COLOR][COLOR=#3364a4]Console[/COLOR][COLOR=#444444].[/COLOR][COLOR=#444444]Write[/COLOR][COLOR=#444444] [/COLOR][COLOR=#444444]([/COLOR][COLOR=#444444][/COLOR][COLOR=#444444]s[/COLOR][COLOR=#444444][[/COLOR][COLOR=#444444]i[/COLOR][COLOR=#444444]])[/COLOR][COLOR=#444444];[/COLOR]
[COLOR=#444444]                    [/COLOR][COLOR=#444444]}
}[/COLOR][/FONT]
 
Strings are immutable so you can't change the characters it contains. You can call ToCharArray to create an array of Chars, loop through that and make your replacements, then create a new String from the result. In that case you'd basically set an element in exactly the same way as you test one, except using an assignment operator (=) instead of an equality operator (==).
 
Back
Top Bottom