Bird
0
0
DSA Cprogramming~30 mins

First Non Repeating Character Using Hash in DSA C - Build from Scratch

Choose your learning style9 modes available
First Non Repeating Character Using Hash
📖 Scenario: Imagine you are building a simple text analyzer that finds the first character in a word that does not repeat. This can help in games like word puzzles or in text processing tools.
🎯 Goal: You will write a C program that finds the first non-repeating character in a given string using a hash (array) to count character occurrences.
📋 What You'll Learn
Create a character array called input with the exact value "swiss"
Create an integer array called count of size 256 initialized to 0
Use a for loop with variable i to count occurrences of each character in input
Use a for loop with variable i to find the first character with count 1
Print the first non-repeating character using printf
💡 Why This Matters
🌍 Real World
Finding the first non-repeating character is useful in text analysis, spell checking, and game development.
💼 Career
This technique helps in coding interviews and software development tasks involving string processing and hashing.
Progress0 / 4 steps
1
Create the input string
Create a character array called input and set it to the exact string "swiss"
DSA C
Hint

Use char input[] = "swiss"; to create the string.

2
Create and initialize the count array
Add an integer array called count of size 256 and initialize all elements to 0
DSA C
Hint

Use int count[256] = {0}; to create and initialize the array.

3
Count character occurrences
Use a for loop with variable i to iterate over input until the null character '\0' and increment count[input[i]] for each character
DSA C
Hint

Use a for loop to go through each character until '\0' and increase the count for that character.

4
Find and print the first non-repeating character
Use a for loop with variable i to iterate over input until '\0'. Inside the loop, check if count[input[i]] is 1. If yes, print the character using printf("%c\n", input[i]) and break the loop.
DSA C
Hint

Loop again through input and print the first character whose count is 1.