Hey everyone!
So last time, me, Aarav, and Rahul were coding Binary Search for numbers — and Aarav proudly said, “Now nothing can stop us!”
But then Rahul smirked and said, “Cool… but can your Binary Search handle words, Mr. Genius?”
And that’s how today’s experiment was born Binary Search on Strings!
What It Does
This program performs a Binary Search on an array of strings.
First, it takes a bunch of words from you (like “apple”, “banana”, “cherry”… ).
Then it sorts them alphabetically using Bubble Sort — because Binary Search only works on sorted lists (Aarav learned that the hard way ).
Once sorted, it uses the divide-and-conquer trick:
Check the middle word
If it matches, yay, found it!
If not, search the left or right half based on alphabetical order.
It keeps doing that until the word is found or until Rahul yells, “It’s not there, bro!”
And the best part? You can keep searching for new words until you say “no.”
How It Works (Friend Style)
I (Daksh) handled the coding — and yes, I fought with string functions again
Aarav said “We should use Bubble Sort!” like he just invented it.
Rahul kept testing it by typing random words like “zebra,” “yak,” and “applepie”
Together, we built a Binary Search that’s as efficient as it is fun!
Code:
#include <stdio.h> #include <string.h> int main() { char stringArray[100][100], searchString[100], temporaryString[100]; int totalStrings, outerIndex, innerIndex, lowIndex, highIndex, middleIndex, foundFlag; char choice[10]; do { printf("Enter the number of strings: "); scanf_s("%d", &totalStrings); printf("Enter %d strings:\n", totalStrings); for (outerIndex = 0; outerIndex < totalStrings; outerIndex++) { scanf_s("%s", stringArray[outerIndex], (unsigned)_countof(stringArray[outerIndex])); } // Sort the strings alphabetically (Bubble Sort) for (outerIndex = 0; outerIndex < totalStrings - 1; outerIndex++) { for (innerIndex = 0; innerIndex < totalStrings - outerIndex - 1; innerIndex++) { if (strcmp(stringArray[innerIndex], stringArray[innerIndex + 1]) > 0) { strcpy_s(temporaryString, sizeof(temporaryString), stringArray[innerIndex]); strcpy_s(stringArray[innerIndex], sizeof(stringArray[innerIndex]), stringArray[innerIndex + 1]); strcpy_s(stringArray[innerIndex + 1], sizeof(stringArray[innerIndex + 1]), temporaryString); } } } printf("\nEnter the string to search: "); scanf_s("%s", searchString, (unsigned)_countof(searchString)); lowIndex = 0; highIndex = totalStrings - 1; foundFlag = 0; while (lowIndex <= highIndex) { middleIndex = (lowIndex + highIndex) / 2; if (strcmp(stringArray[middleIndex], searchString) == 0) { foundFlag = 1; break; } else if (strcmp(stringArray[middleIndex], searchString) < 0) { lowIndex = middleIndex + 1; } else { highIndex = middleIndex - 1; } } if (foundFlag == 1) { printf("\nString \"%s\" found at position %d.\n", searchString, middleIndex + 1); } else { printf("\nString \"%s\" not found in the array.\n", searchString); } printf("\nDo you want to search another set of strings? (yes/no): "); scanf_s("%s", choice, (unsigned)_countof(choice)); } while (strcmp(choice, "yes") == 0 || strcmp(choice, "YES") == 0); printf("\nOkay, exiting the Binary Search (String) program.\n"); return 0; }
Output:
| ← Previous | 🏠 Homepage | Next Chapter → |
No comments:
Post a Comment