hello everyone
In our previous post, we created a simple Linear Search program in C it could find a number in an array and tell us exactly where it’s sitting.
But now we’ve made it better!
Instead of running the program again and again, this version lets you keep searching as long as you want just by typing “yes” or “no.”
What’s New?
Earlier, we could ask our friends (array) just once.
Now, after every search, the program politely asks:
“Do you want to search again? (yes/no):”
If you say “yes”, the friends get ready for another round.
If you say “no”, they wave goodbye and the program exits gracefully.
How It Works
Imagine your group of friends standing in a line, each holding a unique number card.
Your mission? To find which friend is holding the number you’re looking for!
You ask the first friend, “Hey, do you have 20?”
If not, you move to the next friend, and the next until one finally shouts,
“YES, that’s me!”
That’s exactly how Linear Search works you keep checking each friend (array element) one by one until you find the match.
Now in this upgraded version, you can keep asking them multiple times like a quiz night that never ends!
Code:
#include <stdio.h> #include <string.h> int main() { int numbers[100]; // Array to store numbers int totalElements; // Total number of elements in the array int index; // Loop counter int targetValue; // The value to search for int isFound; // Flag to check if value is found char choice[10]; // To store yes/no response // Input: Total number of elements printf("Enter the number of elements in the array: "); scanf("%d", &totalElements); // Input: Elements of the array printf("Enter %d elements:\n", totalElements); for(index = 0; index < totalElements; index++) { scanf("%d", &numbers[index]); } // Start searching loop do { isFound = 0; // reset before each search // Input: The value to be searched printf("Enter the value to search: "); scanf("%d", &targetValue); // Linear Search Logic for(index = 0; index < totalElements; index++) { if(numbers[index] == targetValue) { printf("Value %d found at position %d.\n", targetValue, index + 1); isFound = 1; break; } } // If value not found if(!isFound) { printf("Value %d not found in the array.\n", targetValue); } // Ask user if they want to continue printf("Do you want to search again? (yes/no): "); scanf("%s", choice); } while(strcmp(choice, "yes") == 0 || strcmp(choice, "YES") == 0); printf("Okay, exiting the search.\n"); return 0; }
Output
Now Let’s Search for Words Too!
What if instead of numbers, your friends hold name cards like “apple,” “banana,” or “mango”?
You can now search through these word friends too!
This program searches for strings (words) using the same Linear Search idea just with a little twist using
strcmp()for comparison.How It Works
All your friends (numbers) line up.
You shout out a number you’re looking for.
Each friend checks their card and replies until someone says, “I’ve got it!”
When the round ends, you can choose to play again or call it a night.
That’s how this program uses a loop and the
strcmp()function to make the search repeatable and interactive.Code:#include <stdio.h>#include <string.h>int main() {char words[100][100]; // Array to store wordschar searchWord[100]; // Word to search forint totalWords; // Total number of wordsint position; // Loop counterint isFound; // Flag to check if word foundchar choice[10]; // To store yes/no answer// Input: number of wordsprintf("Enter the number of words: ");scanf("%d", &totalWords);// Input: wordsprintf("Enter %d words:\n", totalWords);for(position = 0; position < totalWords; position++) {scanf("%s", words[position]);}// Repeat searching until user says "no"do {isFound = 0; // Reset before each search// Input: word to searchprintf("Enter the word to search: ");scanf("%s", searchWord);// Linear Search logicfor(position = 0; position < totalWords; position++) {if(strcmp(words[position], searchWord) == 0) {printf("Word '%s' found at position %d.\n", searchWord, position + 1);isFound = 1;break;}}// If not foundif(!isFound) {printf("Word '%s' not found in the list.\n", searchWord);}// Ask user if they want to continueprintf("Do you want to search again? (yes/no): ");scanf("%s", choice);} while(strcmp(choice, "yes") == 0 || strcmp(choice, "YES") == 0);printf("Okay, exiting the search.\n");return 0;}Output
| ← Previous | 🏠 Homepage | Next Chapter → |
No comments:
Post a Comment