Showing posts with label Search. Show all posts
Showing posts with label Search. Show all posts

Dictionary Word Search C Program

How to write a C Program to Find Dictionary Word Search ?


Solution For C Program to Find a Word in Dictionary :

C Depth First Search

How to write a C Program Depth First Search in C Programming Language ?


Solution For C Program :

C Program To Search A Number Inside The Array

How to write a C Program To Search A Number Inside The Array in C Programming Language ?


Solution For C Program :

/*C Program To Search A Number Inside The Array.*/

#include<stdio.h>
#include<conio.h>
void main()
    {
    int i,num,flag=0,arr[5];
    printf("Enter 5 numbers for array:=\n");
    for(i=0;i<5;i++)
        {
        scanf("%d",&arr[i]);
        }
    printf("Enter number u want to search:=");
    scanf("%d",&num);
    for(i=0;i<5;i++)
        {
        if(arr[i]==num)
        {
        printf("number %d do exist in present array",num);
        flag=1;
        break;
        }
        }
        if(flag==0)
        {
        printf("number does not exist in the present array");
        }
    getch();
    }


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable



C Program To Write Data In A File And Search Data From File

How to write a C Program To Write Data In A File And Search Data From File in C Programming Language ?


Solution For C Program :

/*C Program To Write Data In A File And Search Data From File.*/


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main()
{
int r,found;
struct stud
    {
    char name[30];
    int age;
    int roll_no;
    }st;
FILE *fp;
fp=fopen("test.dat","r+t");
        if(fp==NULL)
            {
            printf("\nCannot open file\n");
            exit(1);
            }

printf("enter role no:");
found=0;
scanf("%d",&r);

while((!feof(fp))&&(found))
    {
    fread(&st,sizeof(stud),1,fp);
    if(st.roll_no==r)
    {
    fseek(fp,-sizeof(stud),SEEK_CUR);
    printf("enter new name\n");
    scanf("%s",st.name);
    fwrite(fp,sizeof(stud),1,fp);
    found=1;
    }
    }
    if(!found)
    printf("record not present");
    fclose(fp);
}

You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable


C Program to search an element using linear search or binary search (menu driven program)

How to write a C Program to search an element using linear search or binary search (menu driven program) in C Programming Language ?


This C Program to search an element using linear search or binary search (menu driven program).

Solution:


  1. #include<stdio.h>
  2. void main()
  3. {
  4. int a[25],beg,item,last,n,num,i,ch,mid,f=0;
  5. printf("menu\n");
  6. printf("\n 1.linear search");
  7. printf("\n 2.binary search");
  8. printf("\n enter the choice");
  9. scanf("%d",&ch);
  10. if(ch==1)
  11. {
  12. printf("\n enter the number of elements in the array");
  13. scanf("%d",&n);
  14. printf("\n enter the sorted array");
  15. for(i=0;i<n;i++)
  16. scanf("%d",&a[i]);
  17. printf("\n enter the item to be searched");
  18. scanf("%d",&item);
  19. for(i=0;i<n;i++)
  20. {
  21. if(a[i]==item)
  22. {
  23. printf("\n item found at position%d",i+1);
  24. break;
  25. }
  26. }
  27. if(i==n)
  28. printf("\n item not found");
  29. }
  30. if(ch==2)
  31. {
  32. printf("\nenter the number of elements in the array");
  33. scanf("%d",&n);
  34. printf("enter the sorted array");
  35. for(i=0;i<n;i++)
  36. scanf("%d",&a[i]);
  37. printf("item to be searched");
  38. scanf("%d",&item);
  39. last=n-1;
  40. mid=(beg+last)/2;
  41. while(beg<=last)
  42. {
  43. if(item==a[mid])
  44. {
  45. printf("\n item found at position %d",mid+1);
  46. break;
  47. }
  48. else if(a[mid]>item)
  49. last=mid-1;
  50. else beg=mid+1;
  51. mid=(beg+last)/2;
  52. }
  53. }
  54. }

C Program Performs a search and replace for a specified target and replacement string

How to write a C Program performs a search and replace for a specified target and replacement string. It will prompt for a string to be searched, a target string to be replaced, and then prompt for a replacement string to replace each target occurrence. The program will repeat, asking for three new strings until a NULL string is entered for the string to be searched, ie. the <Enter Key> only being pressed in C Programming Language ?


Solution:

  1. /*Description: This program performs a search and replace for a specified target and replacement string. It will prompt for a string to be searched, a target string
  2.                          to be replaced, and then prompt for a replacement string to replace each target occurrence. The program will repeat, asking for three new strings until
  3.                          a NULL string is entered for the string to be searched, ie. the <Enter Key> only being pressed.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #define MAX_STR 100
  11.  
  12.  
  13. int main(void)
  14. {
  15.         //Variable declarations
  16.         char document[MAX_STR], target[MAX_STR], replacement[MAX_STR];
  17.  
  18.         //Welcome user
  19.         printf("This program performs a search and replace for a specified target and\nreplacement string. It will prompt for a string to be searched, a target string\n");
  20.         printf("to be replaced, and then prompt for a replacement string to replace each target\noccurrence. The program will repeat, asking for three new strings until a\n");
  21.         printf("NULL string is entered for the string to be searched, ie. only the <Enter Key>\nis pressed.\n");
  22.  
  23.         for (;;)
  24.         {
  25.                 //Prompt for and scan in user input
  26.                 printf("\nPlease enter a string to search: ");
  27.                 fgets(document, MAX_STR, stdin);
  28.  
  29.                 //Terminates loop when conditions met
  30.                 if ((strlen(document) == 0) || (strlen(document) == 1))
  31.                         return 0;
  32.                 else
  33.                         document[strlen(document) - 1] = '\0';
  34.  
  35.                 //Continue scanning user input
  36.                 printf("\nPlease enter the target string to search for: ");
  37.                 scanf("%s", &target);
  38.                 printf("\nPlease enter the target replacement string: ");
  39.                 scanf("%s", &replacement);
  40.  
  41.                 //Get string lengths
  42.                 int docL, tarL, repL;
  43.                 docL = strlen(document);
  44.                 tarL = strlen(target);
  45.                 repL = strlen(replacement);
  46.  
  47.                 //Pointer to 1st occurence of target in document
  48.                 char *occurence = strstr(document, target);
  49.                
  50.                 //Checks to see if target string occurs in document string
  51.                 if (occurence == NULL)
  52.                 {
  53.                         printf("\nNo match found!\n");
  54.                         printf("%s\n", document);
  55.                 }
  56.                 else //Replace occurence of target in document
  57.                 {
  58.                         strncpy(occurence, target, tarL);
  59.                         puts(document);
  60.                         printf("%s\n", document);
  61.                 }
  62.         }
  63. }