Data Structure Strings Practice Questions with Solutions

Introductions

Strings are used to store and process text such as names, messages, passwords, and sentences. In data structure practice, string problems help build skills such as indexing, traversal, character counting, searching, reversing, comparison, and checking patterns. The following questions focus on practical problem-solving rather than definitions. They start with simple string operations and gradually introduce common logic used in programming and data structure problems. Data Structure Strings practice questions with solutions help to understand the concepts.

Question 1: Access a Character Using Its Index

Question

Given the following string:

let text = "HELLO";

Find the character at index 2.

Solution

String indexes start from 0.

The string can be represented as:

Index:      0   1   2   3   4
Character:  H   E   L   L   O

The character at index 2 is L.

We can access it using:

let text = "HELLO";

console.log(text[2]);

Output

L

Answer

The character at index 2 is L.


Question 2: Find the Length of a String

Question

Find the number of characters in the following string:

let text = "Data Structures";

Solution

JavaScript provides the length property to find the number of characters.

let text = "Data Structures";

console.log(text.length);

The space between Data and Structures is also counted as a character.

Therefore:

Data = 4 characters
Space = 1 character
Structures = 10 characters

Total:

4 + 1 + 10 = 15

Output

15

Answer

The string contains 15 characters.


Question 3: Count Vowels in a String

Question

Count the number of vowels in the following string:

let text = "education";

Consider a, e, i, o, and u as vowels.

Solution

We need to check every character.

The string is:

e d u c a t i o n

Now check each character:

e → Vowel
d → Not vowel
u → Vowel
c → Not vowel
a → Vowel
t → Not vowel
i → Vowel
o → Vowel
n → Not vowel

The vowels are:

e, u, a, i, o

There are 5 vowels.

We can solve it using a loop:

let text = "education";
let count = 0;

for (let i = 0; i < text.length; i++) {
    if ("aeiou".includes(text[i])) {
        count++;
    }
}

console.log(count);

Output

5

Answer

There are 5 vowels in the string.


Question 4: Reverse a String

Question

Reverse the following string without using the built-in reverse() method:

let text = "HELLO";

Solution

We need to read the string from the last character to the first character.

The indexes are:

Index:      0   1   2   3   4
Character:  H   E   L   L   O

Start from index 4:

O

Then move backward:

O → L → L → E → H

We can create a new string:

let text = "HELLO";
let reversed = "";

for (let i = text.length - 1; i >= 0; i--) {
    reversed += text[i];
}

console.log(reversed);

Output

OLLEH

Answer

The reversed string is OLLEH.


Question 5: Check Whether a Character Exists

Question

Check whether the character "a" exists in the following string:

let text = "Data Structures";

Solution

We need to search through the string for the character "a".

One simple way is to use includes():

let text = "Data Structures";

console.log(text.includes("a"));

The string contains the character a in:

Data

Therefore, the result is true.

Output

true

Answer

Yes, the character a exists in the string.


Question 6: Count a Specific Character

Question

Count how many times the character "a" appears in this string:

let text = "banana";

Solution

Let’s check each character:

b → No
a → Yes
n → No
a → Yes
n → No
a → Yes

The character a appears 3 times.

We can solve this using a loop:

let text = "banana";
let count = 0;

for (let i = 0; i < text.length; i++) {
    if (text[i] === "a") {
        count++;
    }
}

console.log(count);

Output

3

Answer

The character a appears 3 times.


Question 7: Check Whether a String Is a Palindrome

Question

Check whether the following string is a palindrome:

let text = "madam";

A palindrome reads the same from left to right and right to left.

Solution

Original string:

madam

Reverse the string:

madam

Both strings are the same.

We can implement the logic:

let text = "madam";
let reversed = "";

for (let i = text.length - 1; i >= 0; i--) {
    reversed += text[i];
}

if (text === reversed) {
    console.log("Palindrome");
} else {
    console.log("Not a Palindrome");
}

The original string is:

madam

The reversed string is:

madam

Therefore, they are equal.

Output

Palindrome

Answer

madam is a palindrome.


Question 8: Convert a String to Uppercase

Question

Convert the following string to uppercase:

let text = "data structures";

Solution

JavaScript provides the toUpperCase() method.

let text = "data structures";

console.log(text.toUpperCase());

Every lowercase letter is converted into its uppercase equivalent.

data structures
        ↓
DATA STRUCTURES

Output

DATA STRUCTURES

Answer

The uppercase version is DATA STRUCTURES.


Question 9: Find the First Occurrence of a Character

Question

Find the index of the first occurrence of "o" in:

let text = "programming";

Solution

Let’s write the indexes:

Index:      0 1 2 3 4 5 6 7 8 9
Character:  p r o g r a m m i n g

The first "o" appears at index 2.

We can use indexOf():

let text = "programming";

console.log(text.indexOf("o"));

Output

2

Answer

The first occurrence of "o" is at index 2.


Question 10: Count Words in a Sentence

Question

Count the number of words in the following sentence:

let sentence = "I love data structures";

Solution

The sentence contains:

I
love
data
structures

There are 4 words.

We can split the sentence using spaces:

let sentence = "I love data structures";

let words = sentence.split(" ");

console.log(words.length);

The split(" ") operation creates an array:

["I", "love", "data", "structures"]

The array contains 4 elements.

Output

4

Answer

The sentence contains 4 words.

Key Takeaways

  • Strings are sequences of characters.
  • String indexes generally start from 0.
  • Use string[index] to access a character.
  • Use string.length to find the number of characters.
  • String traversal allows us to process characters one by one.
  • Loops can be used to count vowels and specific characters.
  • A string can be reversed by traversing it from the last character to the first.
  • A palindrome reads the same forward and backward.
  • includes() can check whether a character or substring exists.
  • indexOf() can find the position of the first occurrence.
  • split() can divide a sentence into smaller parts such as words.
  • Basic string problems are useful for developing logic needed for more advanced data structure problems.

FAQs

1. What is a string in data structures?

A string is a sequence of characters used to represent text, such as names, sentences, messages, or other textual information.

2. Does string indexing start from 0 in JavaScript?

Yes. JavaScript uses zero-based indexing, so the first character has index 0.

3. How can I find the length of a string?

Use the length property:

let text = "Hello";

console.log(text.length);

The output is 5.

4. How can I reverse a string?

You can traverse the string from the last index to the first and build a new string. This is a useful practice problem for understanding string indexing and traversal.

5. How can I check whether a string is a palindrome?

Reverse the string and compare it with the original string. If both are equal, the string is a palindrome.

6. What is the time complexity of traversing a string?

If a string contains n characters and every character is visited once, the traversal generally takes O(n) time.

7. Why are string problems important in data structures?

String problems improve skills such as indexing, traversal, searching, counting, comparison, and pattern checking. These concepts are useful in many programming and algorithm problems.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top