Difference between revisions of "Testing if a character is in a word"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with " Console.WriteLine("please enter a word:"); string word = Console.ReadLine(); string vowels = "aeiou"; foreach(char c in word) { if(vowels.Contains(c.ToString()...")
 
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
Console.WriteLine("please enter a word:");
+
You can use the `Contains` method of any string variable to check if part of a string(or a single character) is in the original string.
string word = Console.ReadLine();
+
 
string vowels = "aeiou";
+
<syntaxhighlight lang=csharp line>
 +
Console.WriteLine("please enter a word:");
 +
string word = Console.ReadLine();
 +
Console.WriteLine("please enter part of the word:");
 +
string part  = Console.ReadLine();
 +
 
 +
if(word.Contains(part))
 +
{
 +
Console.WriteLine("Part found");
 +
}
 +
else
 +
{
 +
Console.WriteLine("Part not found: ");
 +
}
 +
 
 +
Console.ReadLine();
 +
</syntaxhighlight>
 +
 
 +
Line 6 above uses the `Contains` method of `word`, the parameter required obviously what you are looking for ie `part`. The contains method will return `true` or `false`, so an if statement could be used.
 +
 
 +
==Looking for vowels==
 +
<syntaxhighlight lang=csharp line>
 +
Console.WriteLine("please enter a word:");
 +
string word = Console.ReadLine();
 +
string vowels = "aeiou";
 
 
foreach(char c in word)
+
foreach(char c in word)
{
+
{
if(vowels.Contains(c.ToString()))
+
if(vowels.Contains(c.ToString()))
{
+
{
Console.WriteLine("Vowel found: "+c);
+
Console.WriteLine("Vowel found: "+c);
}
+
}
}
+
}
+
Console.ReadLine();
+
Console.ReadLine();
 +
</syntaxhighlight>
 +
 
 +
The above code uses the `Contains` method again. This time line 3 declares a string variable called vowels.
 +
 
 +
You can then check if a character is in this string. the `ToString()` method is needed because `c` is a char.

Latest revision as of 13:43, 15 June 2023

You can use the `Contains` method of any string variable to check if part of a string(or a single character) is in the original string.

 1 Console.WriteLine("please enter a word:");
 2 string word = Console.ReadLine();
 3 Console.WriteLine("please enter part of the word:");
 4 string part  = Console.ReadLine();
 5 
 6 if(word.Contains(part))
 7 {
 8 	Console.WriteLine("Part found");
 9 }
10 else
11 {
12 	Console.WriteLine("Part not found: ");
13 }
14 
15 Console.ReadLine();

Line 6 above uses the `Contains` method of `word`, the parameter required obviously what you are looking for ie `part`. The contains method will return `true` or `false`, so an if statement could be used.

Looking for vowels

 1 Console.WriteLine("please enter a word:");
 2 string word = Console.ReadLine();
 3 string vowels = "aeiou";
 4 		
 5 foreach(char c in word)
 6 {
 7 	if(vowels.Contains(c.ToString()))
 8 	{
 9 		Console.WriteLine("Vowel found: "+c);
10 	}
11 }
12 	
13 Console.ReadLine();

The above code uses the `Contains` method again. This time line 3 declares a string variable called vowels.

You can then check if a character is in this string. the `ToString()` method is needed because `c` is a char.