Accessing characters by index in a String Last Updated : 02 Jan, 2024 Comments Improve Suggest changes Like Article Like Report Given two strings str1 and an index, the task is to access the character at this index. Examples: Input: str = "hello", index = 1Output: e Input: str = "GfG", index = 2Output: G Accessing characters by index in a string:Individual characters in a string can be accessed by specifying the string name followed by a number in square brackets ( [] ). Below is the implementation for the above approach: C++ // C++ code for the above approach: #include <iostream> using namespace std; int main() { string s = "GfG"; int index = 2; cout << s[index]; return 0; } C // C program to access character by index #include <stdio.h> #include <string.h> int main() { char s[] = "GfG"; int index = 2; printf("%c", s[index]); return 0; } Java /*package whatever //do not write package name here */ import java.io.*; class GFG { public static void main(String[] args) { String s = "GfG"; int index = 2; System.out.println(s.charAt(index)); } } Python3 # declaring the string s = "GfG" index = 2 # accessing the character of s at index print(s[2]) # The code is modified by (omkarchorge10) C# using System; class GFG { static void Main() { string s = "GfG"; // Define an integer 'index' with a value of 2. int index = 2; // Print the character at the specified // index in the string 's'. Console.WriteLine(s[index]); } } JavaScript // Javascript Program to access character by index let str = "GFG" let index = 2; //We can do this by two ways // Using charAt() Function console.log(str.charAt(2)); //Using Indexes console.log(str[2]); OutputG Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Accessing characters by index in a String S srinam Follow Improve Article Tags : Strings DSA Practice Tags : Strings Similar Reads Find last index of a character in a string Given a string str and a character x, find last index of x in str. Examples : Input : str = "geeks", x = 'e' Output : 2 Last index of 'e' in "geeks" is: 2 Input : str = "Hello world!", x = 'o' Output : 7 Last index of 'o' is: 7 Recommended PracticeLast index of a character in the stringTry It! Metho 8 min read Different ways to access characters in a given String in C++ String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character. There are several ways to access substrings and individual characters of a string. The string class supports the following functions for this purpose: operator[]at()subst 4 min read Modifying character in String Given a string str, an integer index, and a character ch, the task is to modify the character at index in a given string and replace it with character ch. Examples: Input: str = "geeksforgeeks", index = 0, ch = 'G'Output: Geeksforgeeks Input: str = "spacing", index = 2, ch = '*'Output: sp*cing Appro 3 min read Remove odd indexed characters from a given string Given string str of size N, the task is to remove the characters present at odd indices (0-based indexing) of a given string. Examples : Input: str = âabcdefâOutput: aceExplanation:The characters 'b', 'd' and 'f' are present at odd indices, i.e. 1, 3 and 5 respectively. Therefore, they are removed f 4 min read Program to Search a Character in a String Given a character ch and a string s, the task is to find the index of the first occurrence of the character in the string. If the character is not present in the string, return -1.Examples: Input: s = "geeksforgeeks", ch = 'k'Output: 3Explanation: The character 'k' is present at index 3 and 11 in "g 6 min read Check if a String contains any index with more than K active characters Given a string S, containing lowercase English alphabets, and an integer K, the task is to find any index of the string which consists of more than K active characters. If found, print Yes. Otherwise, print No. Count of active characters for any index is the number of characters having previous occu 6 min read Replace a character at a specific index in a String in Java In Java, here we are given a string, the task is to replace a character at a specific index in this string. Examples of Replacing Characters in a StringInput: String = "Geeks Gor Geeks", index = 6, ch = 'F'Output: "Geeks For Geeks."Input: String = "Geeks", index = 0, ch = 'g'Output: "geeks"Methods t 3 min read Searching For Characters and Substring in a String in Java Efficient String manipulation is very important in Java programming especially when working with text-based data. In this article, we will explore essential methods like indexOf(), contains(), and startsWith() to search characters and substrings within strings in Java.Searching for a Character in a 5 min read std::basic_string::at in C++ Returns a reference to the character at the specified location pos. The function automatically checks whether pos is the valid position of a character in the string (i.e., whether pos is less than the string length), throwing an out_of_range exception if it is not. Syntax: reference at (size_type po 1 min read Like