Data Structures Practical 3

Question 1
Write a simple C program to perform linear search on an array of Strings to find Case- Insensitive search for a string
searchstr.c
C
				#include <stdio.h>
#include <conio.h>
#include <string.h>

void main() 
{
    char items[5][20] = {"Apple", "baNana", "CHERRY", "orange", "Grapes"};
    char searchStr[20];
    int i, found = 0;

    clrscr();

    printf("\nArray items:\n");
    for(i = 0; i < 5; i++) 
	{
        printf("%s\n", items[i]);
    }

    printf("Enter the string to search (Case-Insensitive): ");
    scanf("%s", searchStr);

    for(i = 0; i < 5; i++) 
	{
        if(strcmpi(items[i], searchStr) == 0) 
		{
            printf("\n%s found at position %d.\n",searchStr, i + 1);
            found = 1;
            break; 
        }
    }

    if(found==0) 
	{
        printf("\n'%s' was not found in the array.", searchStr);
    }

    getch();
}

			
Output
Question 2
Write a simple C program to perform linear search on an array of Strings to find Checking for substring in each string
searchsubstr.c
C
				#include <stdio.h>
#include <conio.h>
#include <string.h>

void main() 
{
    char database[5][30] = 
	{
        "Software Engineering",
        "Computer Science",
        "Database Management",
        "Data Structures",
        "Network Security"
    };
    char searchSub[20];
    int i, found = 0;

    clrscr();

    printf("Available Strings:\n");
    for(i = 0; i < 5; i++) 
	{
        printf("%d. %s\n", i + 1, database[i]);
    }
    printf("\n");

    printf("Enter a substring to search for: ");
    scanf("%s", searchSub);

    for(i = 0; i < 5; i++) 
	{
        if(strstr(database[i], searchSub) != NULL) 
		{
            printf("\n'%s' contains your substring at index %d\n", database[i], i);
            found = 1;
        }
    }

    if(found==0) 
	{
        printf("The substring '%s' was not found in any of the strings.\n", searchSub);
    }

    getch();
}

			
Output

Leave a Comment

Your email address will not be published. Required fields are marked *