Showing posts with label Perform. Show all posts
Showing posts with label Perform. Show all posts

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. }

Menu driven program in C which performs the following operations on strings

How to write a menu driven program in C which performs the following operations on strings:
  1. Write separate function for each option 
  2. Check if one string is sub-string of another string 
  3. Count number of occurrences of a character in the string 
  4. Exit

Solution:

#include<stdio.h>
#include<conio.h>
#include<string.h>
  void substring();
  void char_occurrence();
  void main()
{
  char str[30],str1[30];
  int ch;
  clrscr();
  do
{
  printf("\n****MENU****");
  printf("\n1:Sub String");
  printf("\n2:Character Occurrences");
  printf("\n3:Exit");
  printf("\nEnter your choice: ");
  scanf("%d",&ch);

  switch(ch)
{
  case 1:substring();
  break;

  case 2:char_occurrance();
  break;

  case 3:exit(0);
  break;
}
}
  while(ch!=3);
  getch();
}

  void substring()
{
  char str[30],ch[30];
  fflush(stdin);
  printf("\nEnter the first string: ");
  gets(str);
  fflush(stdin);
  printf("\nEnter the second string: ");
  gets(ch);
  if(strstr(str,ch))
{
  printf("\nThe given string is sub string");
}
  else
{
  printf("\nThe given string is not sub string");
}
}

  void char_occurrennce()
{
  char str2[30],ch;
  int i,count=0;
  fflush(stdin);
  printf("\nEnter the string: ");
  gets(str2);
  fflush(stdin);
  printf("\nEnter the character from string to find occurrence: ");
  scanf("%c",&ch);
  for(i=0;str2[i]!='\0';i++)
{
  if(ch==str2[i])
{
  count++;
}
}
  printf("\nThe occurrence of character is: %d",count);
}

//OUTPUT:
//****MENU****
//1:Sub String
//2:Character Occurrences
//3:Exit

//Enter your choice: 1

//Enter the first string: vinit

//Enter the second string: vinit

//The given string is sub string

//****MENU****
//1:Sub String
//2:Character Occurrences
//3:Exit
//Enter your choice: 2

//Enter the string: vinit

//Enter the character from string to find occurrence: v

//The occurrence of character is: 1

//****MENU****
//1:Sub String
//2:Character Occurrences
//3:Exit
//Enter your choice: 3

Create Two Singly Linked List Perform Differences Display It C Program

How to write a c program to create two singly linked list and perform differencer of two list and display it in C Programming Language ?

Solution:
/*write a c program to create two singly linked list and perform
differences of two list and display it*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*start1=NULL,*start2=NULL,*q1,*q2,*temp1,*temp2;
int n1=0,n2=0,*p1,*p2,i,j;
void create();
void display();
void differ();
void main()
{
clrscr();
create ();
display();
differ();
getch();
}
void create()
{
char ch='y';
while(ch=='y'||ch=='Y')
{
temp1=malloc(sizeof(struct node));
printf("\nenter the number for first L.L:-");
scanf("%d",&temp1->data);
temp1->next=NULL;
n1++;
if(start1==NULL)
start1=temp1;
else
{
q1=start1;
while(q1->next!=NULL)
{
q1=q1->next;
}
q1->next=temp1;
}
printf("\ndo you want to continue(Y|N):-");
scanf("\n%s",&ch);
}
ch='y';
while(ch=='y'||ch=='Y')
{
temp2=malloc(sizeof(struct node));
printf("\nenter the number for second L.L:-");
scanf("%d",&temp2->data);
temp2->next=NULL;
n2++;
if(start2==NULL)
start2=temp2;
else
{
q2=start2;
while(q2->next!=NULL)
{
q2=q2->next;
}
q2->next=temp2;
}
printf("\ndo you want to continue(Y|N):-");
scanf("\n%s",&ch);
}
}
void display()
{
p1=(int *)malloc(n1*sizeof(int));
p2=(int *)malloc(n2*sizeof(int));
if(start1==NULL)
printf("\nlinked list is empty");
else
{
printf("\nelement in first linked list are:-\n");
q1=start1;
for(i=0;i<n1;i++)
{
if(q1!=NULL)
{
*(p1+i)=q1->data;
printf("%d\n",*(p1+i));
q1=q1->next;
}
}
}
if(start2==NULL)
printf("\nlinked list is empty");
else
{
printf("\nelement in second linked list are:-\n");
q2=start2;
for(i=0;i<n2;i++)
{
if(q2!=NULL)
{
*(p2+i)=q2->data;
printf("%d\n",*(p2+i));
q2=q2->next;
}
}
}
}
void differ()
{
printf("\ndifferences of two sets are:-\n");
for(i=0;i<n1;i++)
{
for(j=0;j<n2;j++)
{
if(*(p1+i)==*(p2+j))
break;
}
if(j==n2)
printf("%d\n",*(p1+i));
}
}

Output:
/*
enter the number for first L.L:-4                                              
                                                                               
do you want to continue(Y|N):-y                                                
                                                                               
enter the number for first L.L:-8                                              
                                                                               
do you want to continue(Y|N):-y                                                
                                                                               
enter the number for first L.L:-1                                              
                                                                               
do you want to continue(Y|N):-y                                                
                                                                               
enter the number for first L.L:-6                                              
                                                                               
do you want to continue(Y|N):-n                                                
                                                                               
enter the number for second L.L:-4                                             
                                                                               
do you want to continue(Y|N):-y                                                

enter the number for second L.L:-1                                             
                                                                               
do you want to continue(Y|N):-n                                                
                                                                               
element in first linked list are:-                                             
4                                                                              
8                                                                              
1                                                                              
6                                                                              
                                                                               
element in second linked list are:-                                            
4                                                                              
1                                                                              
                                                                               
differences of two sets are:-                                                  
8
6
*/