Question:7
C program to take input for a string, further take input for a character to search, pass the string to a function, check and print whether the character is present in the string or not?
Sol:
#include<stdio.h> void show(char *p,char ch) { int z=0; while(*p!='\0') { if(*p==ch) { z=1; break; } p++; } if(z==0) printf("character is not present\n"); else printf("character is present\n"); } int main() { char n[20],ch; printf("Enter any string "); gets(n); printf("Enter the character to search "); scanf("%c",&ch); show(n,ch); /* show(&n[0]); */ return(0); }
/* Output */ Enter any string computer Enter the character to search m character is present
Question:8
C program to take input for a string, further take input for a character to search, pass the string to a function, check and print whether the character is present in the string or not, if present print its position and total count?
Sol:
#include<stdio.h> void show(char *p,char ch) { int z=0,i=0; while(*p!='\0') { if(*p==ch) { z=z+1; printf("character found at %d position\n",i+1); } p++; i++; } if(z==0) printf("character is not present\n"); else printf("character is present %d time\n",z); } int main() { char n[20],ch; printf("Enter any string "); gets(n); printf("Enter the character to search "); scanf("%c",&ch); show(n,ch); /* show(&n[0]); */ return(0); }
/* Output */ Enter any string hello Enter the character to search l character found at 3 position character found at 4 position character is present 2 time