Ceng laboratory (1)

Page 1

1 /* 2 * C Program To Count the Occurence of a Substring in String 3 */ 4 #include <stdio.h> 5 #include <string.h> 6 7 char str[100], sub[100]; 8 int count = 0, count1 = 0; 9 10 void main() 11 { 12 int i, j, l, l1, l2; 13 14 printf("\nEnter a string : "); 15 scanf("%[^\n]s", str); 16 17 l1 = strlen(str); 18 19 printf("\nEnter a substring : "); 20 scanf(" %[^\n]s", sub); 21 22 l2 = strlen(sub); 23 24 for (i = 0; i < l1;) 25 { 26 j = 0; 27 count = 0; 28 while ((str[i] == sub[j])) 29 { 30 count++; 31 i++; 32 j++; 33 } 34 if (count == l2) 35 { 36 count1++; 37 count = 0; 38 } 39 else 40 i++; 41 } 42 printf("%s occurs %d times in %s", sub, count1, str); } #include<stdio.h> void Cycle(int *a,int *b,int *c); int main(){ int a,b,c; printf("Enter value of a, b and c respectively: "); scanf("%d%d%d",&a,&b,&c); printf("Value before swapping:\n"); printf("a=%d\nb=%d\nc=%d\n",a,b,c); Cycle(&a,&b,&c); printf("Value after swapping numbers in cycle:\n"); printf("a=%d\nb=%d\nc=%d\n",a,b,c); return 0; }


void Cycle(int *a,int *b,int *c){ int temp; temp=*b; *b=*a; *a=*c; *c=temp; }


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.