Remove all vowels in a string with Java

后端 未结 5 965
不思量自难忘°
不思量自难忘° 2021-01-19 12:46

I am doing a homework assignment for my Computer Science course. The task is to get a users input, remove all of the vowels, and then print the new statement.

I kno

5条回答
  •  遇见更好的自我
    2021-01-19 13:03

    If you want to do it in O(n) time

    • Iterate over the character array of your String
    • If you hit a vowel skip the index and copy over the next non vowel character to the vowel position.
    • You will need two counters, one which iterates over the full string, the other which keeps track of the last vowel position.
    • After you reach the end of the array, look at the vowel tracker counter - is it sitting on a vowel, if not then the new String can be build from index 0 to 'vowelCounter-1'.

    If you do this is in Java you will need extra space to build the new String etc. If you do it in C you can simply terminate the String with a null character and complete the program without any extra space.

提交回复
热议问题