0% found this document useful (0 votes)
14 views53 pages

C Programming Basics and Examples

The document contains multiple C programming examples, including printing messages, taking user input, performing arithmetic operations, checking leap years, and converting binary to decimal. Each code snippet is accompanied by sample outputs and explanations. The programs cover various topics such as loops, conditionals, and basic algorithms.

Uploaded by

abbazkhan93003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views53 pages

C Programming Basics and Examples

The document contains multiple C programming examples, including printing messages, taking user input, performing arithmetic operations, checking leap years, and converting binary to decimal. Each code snippet is accompanied by sample outputs and explanations. The programs cover various topics such as loops, conditionals, and basic algorithms.

Uploaded by

abbazkhan93003
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Statement - Print Hello World

#include <stdio.h>

#include <conio.h>

void main()

clrscr();

printf("Hello world\n");

getch();

Output -

Hello world

Statement - Print Integer

#include <stdio.h>

#include <conio.h>

void main()

int a;

clrscr();

printf("Enter an integer\n");

scanf("%d", &a);

printf("Integer that you have entered is %d\n", a);


getch();

Output -

Enter an integer

Integer that you have entered is 2

Statement - Get input from user

#include <stdio.h>

int main()

int number;

printf( "Please enter a number: " );

scanf( "%d", &number);

printf( "You entered %d", number );

return 0;

Output

Run1 -

Please enter a number: 10

You entered 10

Run2 -

Please enter a number: 20


You entered 20

/*******************************************

Statement - Addition of two number

Programmer - Vineet Choudhary

Written For - [Link]

********************************************/

#include <stdio.h>

#include <conio.h>

void main()

int a, b, c;

clrscr();

printf("Enter two numbers to add\n");

scanf("%d%d",&a,&b);

c = a + b;

printf("Sum of entered numbers = %d\n",c);

getch();

/************************************************

Output -

Enter two numbers to add

Sum of entered numbers = 5

************************************************/

/**********************************************************
Statement - Prefect Leap Year

Programmer - Vineet Choudhary

Written For - [Link]

**********************************************************/

#include <stdio.h>

#include <conio.h>

void main()

int year;

clrscr();

printf("Enter the year : ");

scanf("%d",&year);

int temp=year/100;

if(year%100==0)

if(temp%4==0)

printf("%d is leap year.",year);

else

printf("%d is ordinary year.",year);

else

{
if(year%4==0)

printf("%d is leap year.",year);

else

printf("%d is ordinary year.",year);

getch();

/**************************************************************************

Statement - ATM money dispatch count while currencies are 1000,500 and
100

Programmer - Vineet Choudhary

Written For - [Link]

***************************************************************************
/

#include<stdio.h>

#include<conio.h>

int totalThousand =1000;

int totalFiveFundred =1000;

int totalOneHundred =1000;

void main(){
unsigned long withdrawAmount;

unsigned long totalMoney;

int thousand=0,fiveHundred=0,oneHundred=0;

clrscr();

printf("Enter the amount in multiple of 100: ");

scanf("%lu",&withdrawAmount);

if(withdrawAmount %100 != 0){

printf("Invalid amount;");

getch();

return;

totalMoney = totalThousand * 1000 + totalFiveFundred* 500 +


totalOneHundred*100;

if(withdrawAmount > totalMoney){

printf("Sorry,Insufficient money");

getch();

return;

thousand = withdrawAmount / 1000;

if(thousand > totalThousand)

thousand = totalThousand;

withdrawAmount = withdrawAmount - thousand * 1000;

if (withdrawAmount > 0){


fiveHundred = withdrawAmount / 500;

if(fiveHundred > totalFiveFundred)

fiveHundred = totalFiveFundred;

withdrawAmount = withdrawAmount - fiveHundred * 500;

if (withdrawAmount > 0)

oneHundred = withdrawAmount / 100;

printf("Total 1000 note: %d\n",thousand);

printf("Total 500 note: %d\n",fiveHundred);

printf("Total 100 note: %d\n",oneHundred);

getch();

/*******************************************************

Statement - Display Day of the month.

Programmer - Vineet Choudhary

Written For - [Link]

*******************************************************/

#include<stdio.h>

#include<conio.h>

#include<math.h>

int fm(int date, int month, int year) {

int fmonth, leap;


//leap function 1 for leap & 0 for non-leap

if ((year % 100 == 0) && (year % 400 != 0))

leap = 0;

else if (year % 4 == 0)

leap = 1;

else

leap = 0;

fmonth = 3 + (2 - leap) * ((month + 2) / (2 * month))

+ (5 * month + month / 9) / 2;

//bring it in range of 0 to 6

fmonth = fmonth % 7;

return fmonth;

//----------------------------------------------

int day_of_week(int date, int month, int year) {

int dayOfWeek;

int YY = year % 100;

int century = year / 100;

printf("\nDate: %d/%d/%d \n", date, month, year);

dayOfWeek = 1.25 * YY + fm(date, month, year) + date - 2 * (century


% 4);
//remainder on division by 7

dayOfWeek = dayOfWeek % 7;

switch (dayOfWeek) {

case 0:

printf("weekday = Saturday");

break;

case 1:

printf("weekday = Sunday");

break;

case 2:

printf("weekday = Monday");

break;

case 3:

printf("weekday = Tuesday");

break;

case 4:

printf("weekday = Wednesday");

break;

case 5:

printf("weekday = Thursday");

break;

case 6:

printf("weekday = Friday");

break;

default:

printf("Incorrect data");

return 0;
}

//------------------------------------------

void main() {

int date, month, year;

clrscr();

printf("\nEnter the year ");

scanf("%d", &year);

printf("\nEnter the month ");

scanf("%d", &month);

printf("\nEnter the date ");

scanf("%d", &date);

day_of_week(date, month, year);

getch();

/**********

Output :

Enter the year 2015

Enter the month 12

Enter the date 16

Date: 16/12/2015

weekday = Wednesday
************/

/***************************************************************

Statement - Print 1 to 10 using for loop

Programmer - Vineet Choudhary

Written For - [Link]

Compiler - gcc

***************************************************************/

#include <stdio.h>

int main()

int i;

/* The loop goes while i < 10, and i increases by one every loop*/

for ( i = 0; i < 10; i++ ) {

printf( "%d\n", i );

/*

Output:

3
4

*/

v/**********************************************************

Statement - Print All ASCII Values

Programmer - Vineet Choudhary

Written For - [Link]

**********************************************************/

#include<stdio.h>

#include<conio.h>

void main()

int i = 0;

char ch;

clrscr();

for (i = 0; i < 256; i++)

printf("%c ",ch);

ch = ch + 1;

}
getch();

/***************************************************************

Statement - Check number is prefect number or not.

Programmer - Vineet Choudhary

Written For - [Link]

Compiler - gcc

***************************************************************/

#include <stdio.h>

int main()

//The first perfect number is 6, because 1, 2, and 3

//are its proper positive divisors, and 1 + 2 + 3 = 6.

int n, i = 1, sum = 0;

printf("Enter a number: ");

scanf("%d", &n);

while (i < n)

if (n % i == 0)

sum = sum + i;

}
i++;

if (sum == n)

printf("%d is a perfect number", i);

else

printf("%d is not a perfect number", i);

return 0;

/*

Output

Run1-

Enter a number: 6

6 is a perfect number

Run2-

Enter a number: 36

36 is not a perfect number

*/

/
***************************************************************************
*******
Statement - Convert the given binary number into decimal

Programmer - Vineet Choudhary

Written For - [Link]

***************************************************************************
*******/

#include <stdio.h>

#include <conio.h>

void main()

int num, bnum, dec = 0, base = 1, rem ;

clrscr();

printf("Enter a binary number(1s and 0s)\n");

scanf("%d", &num); /*maximum five digits */

bnum = num;

while( num > 0)

rem = num % 10;

dec = dec + rem * base;

num = num / 10 ;

base = base * 2;

printf("The Binary number is = %d\n", bnum);

printf("Its decimal equivalent is =%d\n", dec);


getch();

} /* End of main() */

/*---------------------------------------------

Output

Enter a binary number(1s and 0s)

10101

The Binary number is = 10101

Its decimal equivalent is =21

----------------------------------------------*/

/*******************************************************

Statement - Print Diamond

Programmer - Vineet Choudhary

Written For - [Link]

*******************************************************/

#include <stdio.h>

#include <conio.h>

void main()

int n, c, k, space = 1;

clrscr();

printf("Enter number of rows\n");

scanf("%d", &n);

space = n - 1;
for (k = 1; k <= n; k++)

for (c = 1; c <= space; c++)

printf(" ");

space--;

for (c = 1; c <= 2*k-1; c++)

printf("*");

printf("\n");

space = 1;

for (k = 1; k <= n - 1; k++)

for (c = 1; c <= space; c++)

printf(" ");

space++;

for (c = 1 ; c <= 2*(n-k)-1; c++)

printf("*");

printf("\n");

}
getch();

/*******************************************************

Statement - Check leap year in a range.

Programmer - Vineet Choudhary

Written For - [Link]

*******************************************************/

#include <stdio.h>

#include <conio.h>

void main(){

int year;

int min_year,max_year;

clrscr();

printf("Enter the lowest year: ");

scanf("%d",&min_year);

printf("Enter the heighest year: ");

scanf("%d",&max_year);

printf("Leap years in given range are: ");

for(year = min_year;year <= max_year; year++){

if(((year%4==0)&&(year%100!=0))||(year%400==0))

printf("%d ",year);

getch();
}

/*

Definition of leap year:

------------------------

Rule 1:

-------

A year is called leap year if it is divisible by 400.

For example:

------------

1600, 2000 etc leap year while 1500, 1700 are not leap year.

Rule 2:

-------

If year is not divisible by 400 as well as 100 but it is divisible by 4 then


that year are also leap year.

For example:

------------

2004, 2008, 1012 are leap year.

Leap year logic or Algorithm of leap year or Condition for leap year:

---------------------------------------------------------------------

IF year MODULER 400 IS 0

THEN leap_year

ELSE IF year MODULER 100 IS 0

THEN not_leap_year

ELSE IF year MODULER 4 IS 0


THEN leap_year

ELSE

not_leap_year

*/

/**********************************************************

Statement - Convert number to binary using bitwise operators

Programmer - Vineet Choudhary

Written For - [Link]

**********************************************************/

#include<stdio.h>

#include<conio.h>

//Display integer number into binary using bitwise operator

void printBinary(int num)

int mask = 0x4000;

if ((num & 0x8000) == 0)

printf("0");

else

printf("1");

while (mask != 0) {

if ((num & mask) == 0)

printf("0");

else

printf("1");

mask = mask >> 1;


}

void main()

int intNum;

clrscr();

printf("\nEnter a integer number :");

scanf("%d", &intNum);

printf("\nInteger number in binary format :");

printBinary(intNum);

getch();

/
***************************************************************************
*******

Statement - Find the value of sin(x) using the series up to the given
accuracy

(without using user defined function). Also print sin(x) using library
function.

Programmer - Vineet Choudhary

Written For - [Link]

***************************************************************************
*******/

#include <stdio.h>
#include <conio.h>

#include <math.h>

#include <stdlib.h>

void main()

int n, x1;

float acc, term, den, x, sinx=0, sinval;

clrscr();

printf("Enter the value of x (in degrees)\n");

scanf("%f",&x);

x1 = x;

/* Converting degrees to radians*/

x = x*(3.142/180.0);

sinval = sin(x);

printf("Enter the accuary for the result\n");

scanf("%f", &acc);

term = x;

sinx = term;

n = 1;

do
{

den = 2*n*(2*n+1);

term = -term * x * x / den;

sinx = sinx + term;

n = n + 1;

} while(acc <= fabs(sinval - sinx));

printf("Sum of the sine series = %f\n", sinx);

printf("Using Library function sin(%d) = %f\n", x1,sin(x));

getch();

} /*End of main() */

/*------------------------------

Output

Enter the value of x (in degrees)

30

Enter the accuary for the result

0.000001

Sum of the sine series = 0.500059

Using Library function sin(30) = 0.500059

RUN 2

Enter the value of x (in degrees)

45

Enter the accuary for the result

0.0001
Sum of the sine series = 0.707215

Using Library function sin(45) = 0.707179

---------------------------------------------*/

/*******************************************************

Statement - Accept a list of data items and find the second largest and
second smallest elements in it.

Programmer - Vineet Choudhary

Written For - [Link]

*******************************************************/

/* Accept a list of data items and find the second

largest and second smallest elements in it. And also computer the
average

of both. And search for the average value whether it is present in the

array or not. Display appropriate message on successful search. */

#include <stdio.h>

#include <conio.h>

void main ()

int number[30];

int i,j,a,n,counter,ave;

clrscr();

printf ("Enter the value of N\n");

scanf ("%d", &n);

printf ("Enter the numbers \n");


for (i=0; i<n; ++i)

scanf ("%d",&number[i]);

for (i=0; i<n; ++i)

for (j=i+1; j<n; ++j)

if (number[i] < number[j])

a = number[i];

number[i] = number[j];

number[j] = a;

printf ("The numbers arranged in descending order are given below\n");

for (i=0; i<n; ++i)

printf ("%d\n",number[i]);

printf ("The 2nd largest number is = %d\n", number[1]);

printf ("The 2nd smallest number is = %d\n", number[n-2]);

ave = (number[1] +number[n-2])/2;

counter = 0;

for (i=0; i<n; ++i)


{

if (ave == number[i])

++counter;

if (counter == 0 )

printf ("The average of %d and %d is = %d is not in the array\n",


number[1], number[n-2], ave);

else

printf ("The average of %d and %d in array is %d in numbers\


n",number[1], number[n-2], counter);

getch();

} /* End of main() */

/*-------------------------------------------------------

Output

Enter the value of N

Enter the numbers

30

80

10

40

70

90

The numbers arranged in descending order are given below

90

80
70

40

30

10

The 2nd largest number is = 80

The 2nd smallest number is = 30

The average of 80 and 30 is = 55 is not in the array

-------------------------------------------------------*/

/*******************************************************

Statement - Copy element of one array into another

Programmer - Vineet Choudhary

Written For - [Link]

*******************************************************/

#include <stdio.h>

#include <string.h>

#include <conio.h>

void main()

int arr1[30], arr2[30], i, num;

clrscr();

printf("\nEnter no of elements :");

scanf("%d", &num);
//Accepting values into Array

printf("\nEnter the values :");

for (i = 0; i < num; i++)

scanf("%d", &arr1[i]);

//Copying data from array 'a' to array 'b'

for (i = 0; i < num; i++)

arr2[i] = arr1[i];

//Printing of all elements of array

printf("The copied array is :");

for (i = 0; i < num; i++)

printf("\narr2[%d] = %d", i, arr2[i]);

getch();

/*******************************************************

Statement - Cyclically permute the elements of an array

Programmer - Vineet Choudhary

Written For - [Link]

*******************************************************/
/* Cyclically permute the elements of an array A. i.e. the content of A1
become that of A2.

And A2 contains that of A3 & so on as An contains A1 */

#include <stdio.h>

#include <conio.h>

void main ()

int i,n,number[30];

clrscr();

printf("Enter the value of the n = ");

scanf ("%d", &n);

printf ("Enter the numbers\n");

for (i=0; i<n; ++i)

scanf ("%d", &number[i]);

number[n] = number[0];

for (i=0; i<n; ++i)

number[i] = number[i+1];

printf ("Cyclically permted numbers are given below \n");

for (i=0; i<n; ++i)

printf ("%d\n", number[i]);


}

getch();

/*-------------------------------------

Output

Enter the value of the n = 5

Enter the numbers

10

30

20

45

18

Cyclically permted numbers are given below

30

20

45

18

10

---------------------------------------------------*/

/**********************************************************

Statement - Delete duplicate elements in an array

Programmer - Vineet Choudhary

Written For - [Link]

**********************************************************/

#include<stdio.h>

#include<conio.h>
void main()

int arr[20], i, j, k, size;

clrscr();

printf("\nEnter array size : ");

scanf("%d", &size);

printf("Enter Numbers : \n");

for (i = 0; i < size; i++)

scanf("%d", &arr[i]);

printf("\nArray with Unique list : ");

for (i = 0; i < size; i++)

for (j = i + 1; j < size;)

if (arr[j] == arr[i])

for (k = j; k < size; k++)

arr[k] = arr[k + 1];

size--;

else
{

j++;

for (i = 0; i < size; i++) {

printf("%d ", arr[i]);

getch();

/*******************************************************

Statement - Delete the specified integer from the list.

Programmer - Vineet Choudhary

Written For - [Link]

*******************************************************/

#include <stdio.h>

#include <conio.h>

void main()

int vectx[10];

int i=0, n=0, pos=0, element=0, found = 0;

clrscr();

printf("Enter how many elements\n");


scanf("%d", &n);

printf("Enter the elements\n");

for(i=0; i<n; i++)

scanf("%d", &vectx[i]);

printf("Input array elements are\n");

for(i=0; i<n; i++)

printf("%d\n", vectx[i]);

printf("Enter the element to be deleted\n");

scanf("%d",&element);

for(i=0; i<n; i++)

if ( vectx[i] == element)

found = 1;

pos = i;

break;

if (found == 1)

{
for(i=pos; i< n-1; i++)

vectx[i] = vectx[i+1];

printf("The resultant vector is \n");

for(i=0; i<n-1; i++)

printf("%d\n",vectx[i]);

else

printf("Element %d is not found in the vector\n", element);

getch();

} /* End of main() */

/*---------------------------------------------------

Output

Run 1

Enter how many elements

Enter the elements

30

10

50
20

40

Input array elements are

30

10

50

20

40

Enter the element to be deleted

35

Element 35 is not found in the vector

Run 2

Enter how many elements

Enter the elements

23

10

55

81

Input array elements are

23

10

55

81

Enter the element to be deleted

55

The resultant vector is

23
10

81

--------------------------------------------------------*/

/**********************************************************

Statement - Find unique element in two arrays

Programmer - Vineet Choudhary

Written For - [Link]

**********************************************************/

#include <stdio.h>

#include <conio.h>

void main() {

int arr[20], i, j, k, size;

clrscr();

printf("\nEnter array size : ");

scanf("%d", &size);

printf("\nAccept Numbers : ");

for (i = 0; i < size; i++)

scanf("%d", &arr[i]);

printf("\nArray with Unique list : ");

for (i = 0; i < size; i++) {

for (j = i + 1; j < size;) {

if (arr[j] == arr[i]) {
for (k = j; k < size; k++) {

arr[k] = arr[k + 1];

size--;

} else

j++;

for (i = 0; i < size; i++) {

printf("%d ", arr[i]);

getch();

/*******************************************************

Statement - Minimum element locationin array

Programmer - Vineet Choudhary

Written For - [Link]

*******************************************************/

#include <stdio.h>

#include <conio.h>

void main()

int array[100], minimum, size, c, location = 1;

clrscr();

printf("Enter the number of elements in array\n");


scanf("%d",&size);

printf("Enter %d integers\n", size);

for ( c = 0 ; c < size ; c++ )

scanf("%d", &array[c]);

minimum = array[0];

for ( c = 1 ; c < size ; c++ )

if ( array[c] < minimum )

minimum = array[c];

location = c+1;

printf("Minimum element is present at location %d and it's value is %d.\


n", location, minimum);

getch();

/**********************************************************

Statement - accept an array of 10 elements and swap 3rd element with


4th element using pointers.

Programmer - Vineet Choudhary

Written For - [Link]

**********************************************************/
#include <stdio.h>

#include <conio.h>

void main()

float x[10];

int i,n;

clrscr();

void swap34(float *ptr1, float *ptr2 ); /* Function Declaration */

printf("How many Elements...\n");

scanf("%d", &n);

printf("Enter Elements one by one\n");

for(i=0;i<n;i++)

scanf("%f",x+i);

swap34(x+2, x+3); /* Function call:Interchanging 3rd element by 4th


*/

printf("\nResultant Array...\n");

for(i=0;i<n;i++)

{
printf("X[%d] = %f\n",i,x[i]);

getch();

} /* End of main() */

/* Function to swap the 3rd element with the 4th element in the array */

void swap34(float *ptr1, float *ptr2 ) /* Function Definition */

float temp;

temp = *ptr1;

*ptr1 = *ptr2;

*ptr2 = temp;

} /* End of Function */

/*-------------------------------------------

Output

How many Elements...

10

Enter Elements one by one

10

20

30

40

50

60

70
80

90

100

Resultant Array...

X[0] = 10.000000

X[1] = 20.000000

X[2] = 40.000000

X[3] = 30.000000

X[4] = 50.000000

X[5] = 60.000000

X[6] = 70.000000

X[7] = 80.000000

X[8] = 90.000000

X[9] = 100.000000

----------------------------------------------------*/

---------------string-----------------

/*******************************************************

Statement - Check whether two strings are anagrams or not

Programmer - Vineet Choudhary

Written For - [Link]

*******************************************************/

/*

What is anagrams?

Two words are said to be anagrams of each other if the letters from one
word can be rearranged to form the other word. From the above definition
it is clear that two strings are anagrams if all characters in both strings
occur same number of times. For example "abc" and "cab" are anagram
strings, here every character 'a', 'b' and 'c' occur only one time in both
strings

*/

#include <stdio.h>

int check_anagram(char [], char []);

void main()

char a[100], b[100];

int flag;

clrscr();

printf("Enter first string\n");

gets(a);

printf("Enter second string\n");

gets(b);

flag = check_anagram(a, b);

if (flag == 1)

printf("\"%s\" and \"%s\" are anagrams.\n", a, b);

else

printf("\"%s\" and \"%s\" are not anagrams.\n", a, b);

getch();

}
int check_anagram(char a[], char b[])

int first[26] = {0}, second[26] = {0}, c = 0;

while (a[c] != '\0')

first[a[c]-'a']++;

c++;

c = 0;

while (b[c] != '\0')

second[b[c]-'a']++;

c++;

for (c = 0; c < 26; c++)

if (first[c] != second[c])

return 0;

return 1;

/*******************************************************
Statement - Find a frequenct of character in string

Programmer - Vineet Choudhary

Written For - [Link]

*******************************************************/

#include <stdio.h>

#include <string.h>

#include <conio.h>

void find_frequency(char [], int []);

void main()

char string[100];

int c, count[26] = {0};

clrscr();

printf("Input a string\n");

gets(string);

find_frequency(string, count);

printf("Character Count\n");

for (c = 0 ; c < 26 ; c++)

printf("%c \t %d\n", c + 'a', count[c]);

}
getch();

void find_frequency(char s[], int count[]) {

int c = 0;

while (s[c] != '\0') {

if (s[c] >= 'a' && s[c] <= 'z' )

count[s[c]-'a']++;

c++;

/*******************************************************

Statement - Insert substring into a string

Programmer - Vineet Choudhary

Written For - [Link]

*******************************************************/

#include <stdio.h>

#include <conio.h>

#include <string.h>

#include <stdlib.h>

void insert_substring(char*, char*, int);

char* substring(char*, int, int);

void main()

{
char text[100], substring[100];

int position;

clrscr();

printf("Enter some text\n");

gets(text);

printf("Enter the string to insert\n");

gets(substring);

printf("Enter the position to insert\n");

scanf("%d", &position);

insert_substring(text, substring, position);

printf("%s\n",text);

getch();

void insert_substring(char *a, char *b, int position)

char *f, *e;

int length;

length = strlen(a);

f = substring(a, 1, position - 1 );

e = substring(a, position, length-position+1);


strcpy(a, "");

strcat(a, f);

free(f);

strcat(a, b);

strcat(a, e);

free(e);

char *substring(char *string, int position, int length)

char *pointer;

int c;

pointer = malloc(length+1);

if( pointer == NULL )

exit(EXIT_FAILURE);

for( c = 0 ; c < length ; c++ )

*(pointer+c) = *((string+position-1)+c);

*(pointer+c) = '\0';

return pointer;

/*******************************************************

Statement - Substring from a position to length


Programmer - Vineet Choudhary

Written For - [Link]

*******************************************************/

#include <stdio.h>

#include <conio.h>

void main()

char string[1000], sub[1000];

int position, length, c = 0;

clrscr();

printf("Input a string\n");

gets(string);

printf("Enter the position and length of substring\n");

scanf("%d%d", &position, &length);

while (c < length) {

sub[c] = string[position+c-1];

c++;

sub[c] = '\0';

printf("Required substring is \"%s\"\n", sub);

getch();

}
/**********************************************************

Statement - check if a string is a subsequence of another string

Programmer - Vineet Choudhary

Written For - [Link]

**********************************************************/

/*

C program to check Subsequence, don't confuse subsequence with


substring.

In our program we check if a string is a subsequence of another string.

User will input two strings and we find if one of the strings is a

subsequence of other. Program prints yes if either first string is a

subsequence of second or second is a subsequence of first. We pass


smaller

length string first because our function assumes first string is of smaller

or equal length than the second string.

*/

#include <stdio.h>

#include <string.h>

#include <conio.h>

int check_subsequence (char [], char[]);

void main () {

int flag;

char s1[1000], s2[1000];

clrscr();
printf("Input first string\n");

gets(s1);

printf("Input second string\n");

gets(s2);

/** Passing smaller length string first */

if (strlen(s1) < strlen(s2))

flag = check_subsequence(s1, s2);

else

flag = check_subsequence(s2, s1);

(flag)?printf("YES\n"):printf("NO\n");

getch();

int check_subsequence (char a[], char b[]) {

int c, d;

c = d = 0;

while (a[c] != '\0') {

while ((a[c] != b[d]) && b[d] != '\0') {

d++;

if (b[d] == '\0')

break;
d++;

c++;

if (a[c] == '\0')

return 1;

else

return 0;

/*

The logic of function is simple we keep on comparing characters of two


strings,

if mismatch occur then we move to next character of second string and if


characters

match indexes of both strings is increased by one and so on. If the first
string ends

then it is a subsequence otherwise not.

*/

/*******************************************************

Statement - Delete vowels from a string

Programmer - Vineet Choudhary

Written For - [Link]

*******************************************************/

#include <stdio.h>

#include <string.h>

#include <conio.h>

int check_vowel(char);
void main()

char s[100], t[100];

int i, j = 0;

clrscr();

printf("Enter a string to delete vowels\n");

gets(s);

for(i = 0; s[i] != '\0'; i++) {

if(check_vowel(s[i]) == 0) { //not a vowel

t[j] = s[i];

j++;

t[j] = '\0';

strcpy(s, t); //We are changing initial string

printf("String after deleting vowels: %s\n", s);

getch();

int check_vowel(char c)

{
switch(c) {

case 'a':

case 'A':

case 'e':

case 'E':

case 'i':

case 'I':

case 'o':

case 'O':

case 'u':

case 'U':

return 1;

default:

return 0;

You might also like