Skip to main content

Posts

Showing posts with the label sample c code

Sample C Program To Find Out The No. Of Times A Character Is Present In The String.

This is a Sample C Program To Accept A String & Find Out Whether This Character Is Present In The String. If Present Then Display How Many Times This Character Occurs. #include<stdio.h> #include<conio.h> #include<string.h> void s(char *p,char hc); void main() { char str[10]; char ch; clrscr(); printf(" \n \n Enter the string: \t "); gets(str); printf(" \n \n Enter the character: \t "); scanf("%c",&ch); s(str,ch); } void s(char *p,char hc) { int a = 0; int i,l; l = strlen(p); for(i = 0; i < l; i++)   {      if ( p[i] == hc)        {          a++;        }      else          continue;   } printf(" \n \n The Entered Character occurs %d times. ", a); getch(); } OUTPUT:  Enter the string: Googleplus Enter the character: l The Entered Character occurs 2 times. ALSO R...

Sample C Program To Accept A String & Find Out Whether This Character Is Present In The String.

#include<stdio.h> #include<conio.h> #include<string.h> void s(char *p,char hc); void main() { char str[10]; char ch; clrscr(); printf(" \n \n Enter the string: \t "); gets(str); printf(" \n \n Enter the character: \t "); scanf("%c",&ch); s(str, ch); } void s(char *p,char hc) { int a; int i,l; l = strlen(p); for(i = 0; i < l; i++) { if(p[i] == hc)   {      a++;   } else      continue; } if(a >= 1)   {      printf(" \n \n Yes, the entered character is present in the string. ");   } else   {      printf(" \n \n No, the entered character is not present in the string. ");   } getch(); } OUTPUT: Enter the string: Malayalam Enter the character: z No, the entered character is not present in the string. ALSO READ: Sample C Program To Accept A String & Display It's Substring. Sample C Program To Accept A St...

Sample C Program To Accept A String & Display Number Of Each Vowels.

#include<stdio.h> #include<conio.h> #include<string.h> char vowels(char *p); void main() { char str[10]; clrscr(); printf(" \n \n Enter the string: \t "); gets(str); vowels(str); getch(); } char vowels(char *p) { int i, l, v1 = 0, v2 = 0, v3 = 0, v4 = 0, v5 = 0, v6 = 0, v7 = 0, v8 = 0, v9 = 0, v10 = 0; l = strlen(p); printf(" \n \n The number of vowels in the string is: \t "); for( i = 0; i < l; i++ ) { if ( p[i] == 'a' )   {      v1++;   } if ( p[i] == 'e' )   {      v2++;   } if ( p[i] == 'i' )   {      v3++;   } if ( p[i] == 'o' )   {      v4++;   } if ( p[i] == 'u' )   {      v5++;   } if ( p[i] == 'A' )   {      v6++;   } if ( p[i] == 'E' )   {      v7++;   } if ( p[i] == 'I' )   {      v8++; ...

Sample C Program To Accept A String & Display Number Of Vowels.

#include<stdio.h> #include<conio.h> #include<string.h> char vowels(char *p); void main() { char str[10]; clrscr(); printf(" \n \n Enter the string: \t "); gets(str); vowels(str); getch(); } char vowels(char *p) { int i,l,v=0; l = strlen(p); printf(" \n \n The number of vowels in the string is: \t "); for(i = 0 ;i < l ; i++)   {      if ( p[i] == 'a' || p[i] == 'e' || p[i] == 'i' || p[i] == 'o' || p[i] == 'u' ||           p[i] == 'A' || p[i] == 'E' || p[i] == 'I' || p[i] == 'O' || p[i] == 'U' )        {           v++;        }      else           continue;   } printf("%d",v); return 1; } OUTPUT:  Enter the string: Dinosaur The number of vowels in the string is: 4 ALSO READ: Sample C Program To Accept A String & Display Vowels. Sample C Program T...

Sample C Program To Accept A String & Display Vowels.

#include<stdio.h> #include<conio.h> #include<string.h> char vowels(char *p); void main() { char str[20]; clrscr(); printf(" \n \n Enter the string: \t "); gets(str); vowels(str); getch(); } char vowels(char *p) { int i,l; l = strlen(p); printf(" \n \n The vowels in the string are: \t "); for( i = 0; i < l; i++ )  {   if(p[i] == 'a' || p[i] == 'e' || p[i] == 'i' || p[i] == 'o' || p[i] == 'u'|| p[i] == 'A' || p[i] == 'E' || p[i] == 'I' || p[i] == 'O' || p[i] == 'U')    {      printf("%c",p[i]);    }   else      continue;  } return 1; } OUTPUT:  Enter the string: Malayalam The vowels in the string are: a a a ALSO READ: Sample C Program To Swap Two Strings Using strcpy() Function. Sample C Program To Sort A Given Number Of Strings Using strcmp() Function. Sample C Program To Display Array Of Strings. ...

Sample C Program To Accept Two Strings & Display Combination Of Two Strings.

#include<stdio.h> #include<conio.h> #include<string.h> void comb(char *p1, char *p2); void main() { char str1[10]; char str2[10]; clrscr(); printf(" \n \n Enter first string:\t "); fflush(stdin); gets(str1); printf(" \n \n Enter second string: \t "); fflush(stdin); gets(str2); comb(str1,str2); } void comb(char *p1, char *p2) { int i, l1, l2; l1 = strlen(p1); l2 = strlen(p2); printf(" \n \n The combination is: \t "); for( i = 0; i < l1; i++ )  {   printf(" %c ",p1[i]);  } for( i = 0; i < l2; i++ )  {   printf(" %c ",p2[i]);  } getch(); } OUTPUT:  Enter first string: Rahul Enter second string: Kulkarni The combination is: RahulKulkarni ALSO READ: Sample C Program To Swap Two Strings Using strcpy() Function. Sample C Program To Sort A Given Number Of Strings Using strcmp() Function. Sample C Program To Display Array Of Strings. Sample C Program To Co...

Sample C Program To Accept Two Strings & Display The Largest String.

#include<stdio.h> #include<conio.h> #include<string.h> void largstr(char *p1,char *p2); void main() { char str1[10]; char str2[10]; clrscr(); printf(" \n \n Enter first string: \t "); fflush(stdin); gets(str1); printf(" \n \n Enter second string: \t "); fflush(stdin); gets(str2); largstr(str1,str2); } void largstr(char *p1, char *p2) { int l1, l2, i; l1 = strlen(p1); l2 = strlen(p2); printf(" \n \n The largest string is: \t "); if(l1 > l2)  {   for(i = 0; i < l1; i++ )    {      printf(" %c ",p1[i]);    }  } else  {   for(i = 0; i < l2; i++ )    {       printf("%c",p2[i]);    }  } getch(); } OUTPUT:  Enter first string: rahul Enter Second String: ankush The largest string is: ankush ALSO READ: Sample C Program To Swap Two Strings Using strcpy() Function. Sample C Program To Sort A Given Number ...

Sample C Program To Accept A String & Display It's Alternate Characters In Reverse.

#include<stdio.h> #include<conio.h> #include<string.h> char realt(char *p); void main() { char str[10]; clrscr(); printf(" \n \n Enter the string: \t "); gets(str); realt(str); getch(); } char realt(char *p) { int i,l; l = strlen(p); printf(" \n \n Alternate characters in reverse: \t "); for(i = l - 1; i >= 0; i -= 2)  {   printf(" %c ",p[i]);  } return 1; } OUTPUT: Enter the string: pneumonia Alternate characters in reverse: a n m e p ALSO READ: Sample C Program To Swap Two Strings Using strcpy() Function. Sample C Program To Sort A Given Number Of Strings Using strcmp() Function. Sample C Program To Display Array Of Strings. Sample C Program To Convert String To An Integer Using atoi() Function. Sample C Program To Find The Length Of A String.

Sample C Program To Accept A String & Display It's Substring.

#include<stdio.h> #include<conio.h> #include<string.h> char substr(char *p, int s, int n); void main() { char str[50]; int start,no_of_chars; clrscr(); printf(" \n \n Enter the string: \t "); gets(str); printf(" \n \n Enter start position for substring: \t "); fflush(stdin); scanf(" %d ",&start); printf(" \n \n Enter number of characters: \t "); fflush(stdin); scanf(" %d ",&no_of_chars); substr(str,start,no_of_chars); getch(); } char substr(char *p, int s, int n) { int i; printf(" \n \n The substring is: \t "); for( i = s - 1; i < n;i++)  {   printf("%c",p[i]);  } return 1; } OUTPUT:  Enter the string: Please Mention Your Address: Enter start position for substring: 20 Enter number of characters: 7 The substring is: Address ALSO READ: Sample C Program To Swap Two Strings Using strcpy() Function. Sample C Program To Sort A Given Num...

Sample C Program To Accept A String & Display Alternate Characters In Either Case.

#include<stdio.h> #include<conio.h> #include<string.h> char altcase(char *p); void main() { char str[20]; clrscr(); printf(" \n \n Enter the string: \t "); gets(str); altcase(str); getch(); } char altcase(char *p) { printf(" \n \n Alternate characters of string are: \t "); while(*p)  {    if (*p >= 'A' && *p <= 'Z')     {       *p = *p + 32;     }    else if (*p >='a' && *p <='z')     {       *p = *p - 32;     }    printf("%c",*p);    p += 2;  } return 1; } OUTPUT:  Enter the string: LnTInfotech Alternate Characters of string are: L T n o e h ALSO READ: Sample C Program To Swap Two Strings Using strcpy() Function. Sample C Program To Sort A Given Number Of Strings Using strcmp() Function. Sample C Program To Display Array Of Strings. Sample C Program To Conve...

Sample C Program To Accept A String & Display Its Alternate Characters.

#include<stdio.h> #include<conio.h> #include<string.h> char alt(char *p); void main() { char str[10]; clrscr(); printf(" \n \n Enter the string: \t "); gets(str); alt(str); getch(); } char alt(char *p) { int j,l; l = strlen(p); printf(" \n \n Alternate characters of the entered string are: \t "); for(j = 0; j <= l - 1; j = j + 2)   {     printf("%c",p[j]);   } return 1; } OUTPUT: Enter the string: BODYGUARD Alternate characters of the entered string are: B D G A D ALSO READ: Sample C Program To Swap Two Strings Using strcpy() Function. Sample C Program To Sort A Given Number Of Strings Using strcmp() Function. Sample C Program To Display Array Of Strings. Sample C Program To Convert String To An Integer Using atoi() Function. Sample C Program To Find The Length Of A String.

Sample C Program To Accept A String & Display In Reverse.

#include<stdio.h> #include<conio.h> #include<string.h> char reverse(char *p); void main() { int i; char str[10]; clrscr(); printf(" \n \n Enter the String: \t "); gets(str); reverse(str); getch(); } char reverse (char *p) { int j,l; l = strlen(p); printf(" \n \n String in reverse is: \t "); for(j = l - 1; j >= 0; j--) { printf("%c",p[j]); } return 1; } OUTPUT: Enter the string: Kolaveri String in reverse is: irevalok ALSO READ: Sample C Program To Swap Two Strings Using strcpy() Function. Sample C Program To Sort A Given Number Of Strings Using strcmp() Function. Sample C Program To Display Array Of Strings. Sample C Program To Convert String To An Integer Using atoi() Function. Sample C Program To Find The Length Of A String.

Sample C Program To Accept A String & Display It.

#include <stdio.h> #include <conio.h> char string( char str2[10] ); void main() { int i; char str1[10]; clrscr(); printf(" \n \n Enter The String: \t "); for( i = 0; i <= 9; i++ )     scanf(" %c ", &str1[i] ); string( str1 ); getch(); } char string(char str2[10]) { int j; printf(" \n \n The String Is \t "); for( j = 0; j <= 9; j++ )     printf(" %c ", str2[j] ); return 1; } OUTPUT: Enter The String: Interview The String Is: Interview ALSO CHECKOUT: --  C Program To Find The Length Of A String. --  C Program To Concatenate Two Strings. --  C Program To Compare Two Strings. --  C Program To Swap Two Strings. --  C Program To Swap Two Strings Using strcpy() Function. --  C Program To Sort A Given Number Of Strings Using strcmp() Function. --  C Program To Check Whether A String Is Palindrome Or Not. --  C Program To Print The Reverse Of A String. ...

Sample C Program To Compare Two Strings.

#include <stdio.h> #include <conio.h> #include <string.h> int main() { char a[100], b[100]; printf(" Enter the first string: "); gets(a); printf(" Enter the second string: "); gets(b); if( strcmp ( a , b ) == 0 ) printf(" Entered strings are equal. \n "); else printf(" Entered strings are not equal. \n "); getch(); return 0; } OUTPUT: Enter the first string team Enter the second string team Entered strings are equal. Sample C Program To Print The Reverse Of A String. #include <stdio.h> #include <conio.h> #include <string.h> void main() { char arr[100]; printf(" Enter a string to reverse \n "); gets(arr); strrev(arr); printf(" Reverse of entered string is \n %s \n ", arr); getch(); return 0; } OUTPUT: Enter a string to reverse violet Reverse of entered string is teloiv ALSO READ: -- Program to accept a string and print it by...

Sample C Program To Print Sum Of Series 1 + 3 + 5 + …. N.

#include <stdio.h> #include <conio.h> void main() { int n, i, sum = 0; clrscr(); printf(" Enter any number: " ); scanf(" %d ", &n); for(i = 1; i<n; i = i + 2 ) { printf(" %d + ", i); sum = sum + i; } printf(" %d ", n); printf(" \n Sum = %d ", sum + n ); getch(); } OUTPUT: Enter any number: 9 1 + 3 + 5 + 7 + 9 Sum = 25 Sample C Program To Print A Star Pyramid. #include <stdio.h> #include <conio.h> void main() { int row, c, n, temp; printf(" Enter the number of rows in pyramid of stars you wish to see: "); scanf(" %d ", &n); temp = n; for ( row = 1 ; row <= n ; row++ )    {       for ( c = 1 ; c < temp ; c++ )             printf(" ");       temp--;       for ( c = 1 ; c <= 2 * row - 1 ; c++ )             printf(" * ");       printf(" \n ");     } getc...

Sample C Program To Check Whether An Year Is A Leap Year Or Not.

#include <stdio.h> #include <conio.h> void main() { int n; clrscr(); printf(" Enter any year: "); scanf(" %d ", &n); if( n % 4 == 0 ) printf(" Year is a leap year "); else printf(" Year is not a leap year "); getch(); } OUTPUT: Enter any year: 2004 Year is a leap year Sample C Program To Display A Sequence Using Stars. #include <stdio.h> #include <conio.h> void main() { int i, j; clrscr(); for( i = 1; i<=5; i++)    {        for( j = 1; j<=i; j++)              printf(" * ");        printf(" \n ");   } getch(); } OUTPUT: * ** *** **** ***** ALSO READ: --  Program To Print First Ten(10) Natural Numbers. --  Program To Print Fibonacci Series Upto 100. --  Program To Check Whether A Number Is Prime Or Not. --  Program To Compute The Reverse Of A Number. --  Program To Print A Sem...

Sample C Program To Find The Square Of A Number.

#include <stdio.h> #include <conio.h> void main() { int rev( int ); int r, a; clrscr(); printf(" Enter any number: " ); scanf(" %d ", &a); r = rev( a ); printf(" Square is : %d ", r); getch(); } int rev(int x) { return( x * x ); } OUTPUT: Enter any number: 25 Square is : 625 Sample C Program To Find Gross Salary. #include <stdio.h> #include <conio.h> void main() { int gs, bs, da, ta; clrscr(); printf(" Enter basic salary: "); scanf("%d",&bs); da = ( 10 * bs ) / 100; ta = ( 12 * bs ) / 100; gs = bs + da + ta; printf(" Gross salary = %d ", gs); getch(); } OUTPUT: Enter basic salary: 1000 Gross salary = 1220 ALSO READ: --  Program To Print First Ten(10) Natural Numbers. --  Program To Print Fibonacci Series Upto 100. --  Program To Check Whether A Number Is Prime Or Not. --  Program To Compute The Reverse Of A Number. --  Program To ...

Sample C Program To Display The Days In A Week.

#include <stdio.h> #include <conio.h> void main() { char ch; clrscr(); printf(" Enter m for Monday\n t for Tuesday\n w for Wednesday\n h for Thursday\n f for Friday\n s for Saturday\n u for Sunday "); scanf(" %c ", &ch); switch(ch)    {       case 'm':       case 'M':       printf(" The day is Monday ");       break;       case 't':       case 'T':       printf(" The day is Tuesday ");       break;       case 'w':       case 'W':       printf(" The day is Wednesday ");       break;       case 'h':       case 'H':       printf(" The day is Thursday ");       break;       case 'f':       case 'F':       printf(" The day is Friday ");   ...

Sample C Program To Check Whether A Number Is Armstrong Or Not.

#include <stdio.h> #include <conio.h> main() { int n, sum = 0, temp, r; printf( " Enter a number " ); scanf( " %d ", &n); temp = n; while( temp != 0 )     {        r = temp % 10;        sum = sum + r * r * r;        temp = temp / 10;     } if ( n == sum ) printf( " Entered number is an armstrong number. " ); else printf( " Entered number is not an armstrong number. " ); getch(); return 0; } OUTPUT: Enter a number6 Entered number is not an Armstrong number. ALSO READ: --  Program To Print First Ten(10) Natural Numbers. --  Program To Print Fibonacci Series Upto 100. --  Program To Check Whether A Number Is Prime Or Not. --  Program To Compute The Reverse Of A Number. --  Program To Print A Semicolon Without Using A Semicolon. --  Program To Display Function Without Using The Main Function. --  Program ...

Sample C Program To Accept & Add Ten Numbers Using Pointers.

#include <stdio.h> #include <conio.h> void main () { int i, total; int arr[10]; int *a; a = arr; for ( i = 0; i < 10; i++ )    {        printf ( " Enter the number %d: ", i+1 );        scanf ( " %d ", &arr[i] );     } for ( i = 0; i < 10; i++ )     {         printf ( " %d--- ", *a );         total = total + *a;         a = a + 1;     } printf ("\n Total = %d \n",total);  }   OUTPUT:  Enter the number 1: 1 Enter the number 2: 2 Enter the number 3: 3 Enter the number 4: 4 Enter the number 5: 5 Enter the number 6: 6 Enter the number 7: 7 Enter the number 8: 8 Enter the number 9: 9 Enter the number 10: 10 1---2---3---4---5---6---7---8---9---10--- Total = 55 ALSO READ: -- Program To Understand Working Of Address Concept. -- Program On Use Of Array & Pointer Con...