Difference between revisions of "Accessing each character of a string"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "Accessing the individual characters of a string is a common part of any Section B question. This page looks at the different methods you can use to achieve this. ==Method 1==...")
 
Line 3: Line 3:
 
==Method 1==
 
==Method 1==
  
<syntaxhighlight lang=csharp>
+
<syntaxhighlight lang=csharp line>
Console.WriteLine("please enter a word:");
+
Console.WriteLine("please enter a word:");
string word = Console.ReadLine();
+
string word = Console.ReadLine();
 
 
for(int i=0; i < word.Length;i++)
+
for(int i=0; i < word.Length;i++)
{
+
{
Console.WriteLine(word[i]);
+
Console.WriteLine(word[i]);
}
+
}
+
Console.ReadLine();
+
Console.ReadLine();
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 07:31, 15 June 2023

Accessing the individual characters of a string is a common part of any Section B question. This page looks at the different methods you can use to achieve this.

Method 1

1 Console.WriteLine("please enter a word:");
2 string word = Console.ReadLine();
3 		
4 for(int i=0; i < word.Length;i++)
5 {
6 	Console.WriteLine(word[i]);
7 }
8 	
9 Console.ReadLine();