string removeVowels(string s)
{
string c = "";
// Your code goes here
for(int i=0;i<s.length();i++){
if(s[i] != 'A'||s[i] != 'E'||s[i] != 'I'||s[i] !=
'O'||s[i] != 'U'||s[i] != 'a'||s[i] != 'e'||s[i]
!= 'i'||s[i] != 'o'||s[i] != 'u'){
c = c + s[i];
}
}
return c;
}
I have to some vowels from the string why this code not works
Related
i am running a test but i am receiving null after the test and i am receiving a AssertionFailedError
assertEquals("", this.myCustomString.remove(""));
this.myCustomString.setString(null);
assertEquals("", this.myCustomString.remove(""));
this.myCustomString.setString("my lucky numbers are 6, 8, and 19.");
assertEquals("my lucky numbes e 6, 8, nd 19.", this.myCustomString.remove("ra6"));
public String remove(String arg){//removes specified characters from the string
if (myString == null || myString == "") {
return this.myString;
}
if (myString != null) {
this.myString = myString.replaceAll(arg,"");
return myString;
}
return myString;
}
This should help
String result = "";
if (myString == null || myString == "") {
return "";
}
//places the String arg into a character array
char[] c = arg.toCharArray();
// then use a for loop to check if there is only letters in the string arg
// after we have checked to see if there is only letters when remove anything that is not letters
for(int i= 0; i< c.length; i++){
if (Character.isLetter(c[i])) {
result = result + c[i];
}
}
// when then use the result from above and place it into its own character array, we then use the characters
// from the array to be replaced by blank
for (char ch : result.toCharArray()) {
myString = myString.replace(String.valueOf(ch), "");
}
return myString;
}
I'm trying to make a simple function in dart to test on which should remove all vowels from an input string but its seems my code never changes the from the original input. Could anyone help me with this? thanks
String removeVowel( str) {
var toReturn = "";
for (var i = 0; i < str.length; i++) {
var temp = str.substring(i,i+1);
if (temp != 'a' || temp != 'e' || temp != 'i' || temp != 'o' || temp!= 'u')
{
toReturn = toReturn + temp;
}
}
return toReturn;
}
and what my tests shows:
00:02 +0 -1: dog --> dg [E]
Expected: 'dg'
Actual: 'dog'
Which: is different.
Expected: dg
Actual: dog
^
Differ at offset 1
Good first try but there is a much easier way of doing this.
replaceAll should do the trick
String removeVolwels(String s){
return s.replaceAll(RegExp('[aeiou]'),'');
}
https://api.flutter.dev/flutter/dart-core/String/replaceAll.html
To make your code work you should change the || to &&
String removeVowel( str) {
var toReturn = "";
for (var i = 0; i < str.length; i++) {
var temp = str.substring(i,i+1);
if (temp != 'a' && temp != 'e' && temp != 'i' && temp != 'o' && temp!= 'u')
{
toReturn = toReturn + temp;
}
}
return toReturn;
}
The loop keeps repeating the output for the entire span of the word.
I tried initiating the console.write on the inside and outside the loop. I also tried changing the values of the incrementor and the substring.
Console.WriteLine("Enter a word.");
string userWord = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("You wrote {0}", userWord);
Console.WriteLine();
userWord.ToLower();
char[] wordArray = userWord.ToArray();
for (int i = 0; i <= wordArray.Length; i++)
{
string theLetter = userWord.Substring(i, 1);
//theLetter = theLetter.ToLower();
string rebuilt = new string(wordArray);
if (wordArray[i] == 'a' || wordArray[i] == 'e' || wordArray[i] == 'i' || wordArray[i] == 'o' || wordArray[i] == 'u')
{
wordArray[i] = '$';
}
Console.WriteLine("Your word is now: {0}", rebuilt);
Console.WriteLine("The total number of letters in your word is {0}", userWord.Length);
}
Console.ReadLine();
the console.write should display it's output only once.
You would need to move your Console.WriteLine outside the loop. Also, your loop condition needs to be changed to avoid the ArgumentOutOfRangeException.
for (int i = 0; i < wordArray.Length; i++)
There is one more error in your code. You are converting userWord to lower case, but you are not storing the result.
userWord.ToLower();
Above line needs to be replaced with
userWord = userWord.ToLower();
Complete Code
Console.WriteLine("Enter a word.");
string userWord = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("You wrote {0}", userWord);
Console.WriteLine();
userWord = userWord.ToLower();
char[] wordArray = userWord.ToArray();
for (int i = 0; i < wordArray.Length; i++)
{
if (wordArray[i] == 'a' || wordArray[i] == 'e' || wordArray[i] == 'i' || wordArray[i] == 'o' || wordArray[i] == 'u')
{
wordArray[i] = '$';
}
}
var rebuildWord = new string(wordArray);
Console.WriteLine("Your word is now: {0}", rebuildWord);
Console.WriteLine("The total number of letters in your word is {0}", userWord.Length);
Just for your information, you could achieve the same using Regex as well.
Console.WriteLine("Enter a word.");
string userWord = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("You wrote {0}", userWord);
Console.WriteLine();
var rebuildWord = Regex.Replace(userWord,#"[aeiouAEIOU]","$");
Console.WriteLine("Your word is now: {0}", rebuildWord);
Console.WriteLine("The total number of letters in your word is {0}", userWord.Length);
This is just simple. You should check if the loop is in final iteration before printing.
Console.WriteLine("Enter a word.");
string userWord = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("You wrote {0}", userWord);
Console.WriteLine();
userWord.ToLower();
char[] wordArray = userWord.ToArray();
for (int i = 0; i <= wordArray.Length; i++)
{
string theLetter = userWord.Substring(i, 1);
//theLetter = theLetter.ToLower();
string rebuilt = new string(wordArray);
if (wordArray[i] == 'a' || wordArray[i] == 'e' || wordArray[i] == 'i' || wordArray[i] == 'o' || wordArray[i] == 'u')
{
wordArray[i] = '$';
}
if(i==userWord.Length)
{
Console.WriteLine("Your word is now: {0}", rebuilt);
Console.WriteLine("The total number of letters in your word is {0}", userWord.Length);
}
}
Console.ReadLine();
I need to write an application that fist converts a string to unicode and then add 2 to the unicode value to create a new string.
Basically, if the input is: password is RhYxtz, then the output should look like: rcuuyqtf ku TjAzvb
the following code is what I have so far:
public static void main(String[] args){
System.out.print ("Enter text: ");
Scanner scan = new Scanner(System.in);
String text = scan.nextLine();
int length = text.length();
for(int i = 0; i < length; i ++){
char currentChar = text.charAt(i);
int currentChar2 = currentChar+2;
String s = String.format ("\\u%04x", currentChar2);
System.out.println ("Encoded message: " + s);
}
}
The problem is that I don't know how to convert the unicode back into a letter string and how to keep the format the same as the input. Could anyone help me? Thanks.
Unicode code points can be gathered in java 8 as:
public static String encryped(String s) {
int[] cps = s.codePoints()
.mapToInt((cp) -> cp + 2)
.toArray();
return new String(cps, 0, cps.length);
}
or in a loop with codePointAt in earlier versions.
Java char (2 bytes) are UTF-16, and their int value is not always a Unicode symbol aka code point.
Try this:
import java.util.Scanner;
public class Example {
public static void main(String[] args) {
System.out.print ("Enter text: ");
Scanner scan = new Scanner(System.in);
String text = scan.nextLine();
int length = text.length();
String s = "";
for(int i = 0; i < length; i ++){
char currentChar = text.charAt(i);
if (currentChar == ' '){
s += currentChar;
} else {
s += (char) (currentChar + 2);
}
}
System.out.println ("Encoded message: " + s);
}
}
This should work for US ASCII letters:
StringBuilder buf = new StringBuilder(length);
for(int i = 0; i < length; i ++){
char currentChar = text.charAt(i);
if (currentChar < 128 && Character.isLetter(currentChar)) {
if (currentChar == 'y' || currentChar == 'z'
|| currentChar == 'Y' || currentChar == 'Z') {
buf.append((char) (currentChar + 2 - 26));
} else {
buf.append((char) (currentChar + 2));
}
} else {
buf.append(currentChar);
}
}
System.out.println(buf.toString());
def binary_string(s):
Write a function that takes a string, converts it to a binary string
where vowels are replaced by 0 and consonants are replaced by 1.
The function should return the binary string.
binary_string("Karen")
'10101'
binary_string("Hello World!")
'10110 10111!'
if __name__ == '__main__':
import doctest
doctest.testmod(verbose = True)
Try something like this. Assuming the name of your string variable is "stringVar".
string binaryString="";
for(int i=0;i<stringVar.Length();i++)
{
if(stringVar == 'a' || stringVar == 'A' || stringVar == 'e') //and so on
{
binaryString[i] = '0';
}
else
{
binaryString[i] = '1';
}
}//end for loop.
print(binaryString);
This should give you the result you are looking for. This is the basic functionality. The rest depends on the language you are coding in.
Hope this helps.
Since you have not specified language in which you want solution, so i am assuming it to be java(from my side ). Although, algorithm will be valid for all languages.
code
public class SO {
public static void main(String[] args) {
String s = "Karen";
char[] arr = s.toCharArray();
StringBuilder binary = new StringBuilder();
for (char c : arr) {
if(Character.isAlphabetic(c)){
if (isVowel(c)) {
binary.append("0");
}else{
binary.append("1");
}
}else{
binary.append(c);
}
}
System.out.println(binary);
}
private static boolean isVowel(char c) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
return true;
}
return false;
}
}
output
When input is
String s = "Karen";
then output is
binary = 10101
and
When input is
String s = "Hello World!";
then output is
binary = 10110 10111!