0% found this document useful (0 votes)
3 views1 page

C Program for Linear String Search

The document contains a C program that implements a linear search algorithm to find a target string within an array of strings. It prompts the user to enter the number of strings, the strings themselves, and the target string to search for. The program outputs the index of the target string if found, or a message indicating that the string was not found.

Uploaded by

ranimande0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views1 page

C Program for Linear String Search

The document contains a C program that implements a linear search algorithm to find a target string within an array of strings. It prompts the user to enter the number of strings, the strings themselves, and the target string to search for. The program outputs the index of the target string if found, or a message indicating that the string was not found.

Uploaded by

ranimande0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

NAME:RAMKRISHNA ARUTLAWAR printf("String not found.

\n");

BRANCH:IF3K PRAC:3 }

#include <stdio.h> return 0;

#include <string.h> }

#define MAX_STRINGS 100 OUTPUT:

#define MAX_LENGTH 100

int linearSearch(char arr[][MAX_LENGTH], int n,


const char* target) {

for (int i = 0; i < n; i++) {

if (strcmp(arr[i], target) == 0) {

return i;

}}

return -1;

int main() {

char strings[MAX_STRINGS][MAX_LENGTH];

int n;

char target[MAX_LENGTH];

printf("Enter the number of strings: ");

scanf("%d", &n);

printf("Enter the strings:\n");

for (int i = 0; i < n; i++) {

scanf(" %[^\n]", strings[i]); }

printf("Enter the string to search for: ");

scanf(" %[^\n]", target);

int index = linearSearch(strings, n, target);

if (index != -1) {

printf("String found at index %d.\n", index);

} else {

You might also like