Posts

Showing posts from November 24, 2019

C Program To Accept The String That Ends With 01 | C Programming

Image
C Program To Accept The String That Ends With 01 #include <stdio.h> #include <math.h> #include <string.h> int main () {     char s [ 100 ];     int i = 0 , q = 0 , l;     printf ( "Enter stringover {0,1}: \n " );         scanf ( " %s " ,s);     l = strlen (s);     for (i = 0 ; s [i] != ' \0 ' ; i++)     {         if ( s [i] == '0' && q == 0 )         {             if (i == (l - 2 ))                 q = 1 ;             else                 q = 0 ;         }         else if ( s [i] == '1' && q == 0 )             q = 0 ;         else if ( s [i] == '1' && q == 1 )             q = 2 ;     }     if (q == 2 )     {         printf ( "Accepted. \n\n " );     }     else         printf ( "Rejected." );     return 0 ; } OUTPUT #1 OUTPUT #2 Related Posts C Program To Accept The String Having Even Length | C Programming C Program To Accept The String T

C Program To Accept The String Starting With 01 | C Programming

Image
C Program To Accept The String Starting With 01 #include <stdio.h> #include <string.h> int main () {     char str [ 10 ];     int len, i, q = 0 ;     printf ( "enter the string which only contain 0 and 1 \n " );     scanf ( " %s " , str);     len = strlen (str);     for (i = 0 ; i <= len; i++)     {         if ( str [i] == '0' && q == 0 )             q = 1 ;         else if ( str [i] == '1' && q == 0 )             break ;         else if ( str [i] == '1' && q == 1 )             q = 2 ;         else if ( str [i] == '0' && q == 1 )             break ;         else if ( str [i] == '0' && q == 2 )             q = 2 ;         else if ( str [i] == '1' && q == 2 )             q = 2 ;     }     if (q == 2 )         printf ( "Given string is accepted \n " );     else         printf ( "Given string is not accepted \n "

C Program For Bubble Sort | C Programming

Image
C Program For Bubble Sort by Generating Random Numbers and Using time.h Function #include <stdio.h> #include <time.h> #include <stdlib.h> void bubble_sort ( int [], int ); int main () {     int list [ 100 ], n , i ;     time_t t ;     printf ( "Enter the max number \n " );     scanf ( " %d " , & n );     srand (( unsigned ) time (& t ));     for ( i = 0 ; i < n ; i ++)     {         list [ i ] = rand () % 100 ;     }     for ( i = 0 ; i < n ; i ++)     {         printf ( " %d \t " , list [ i ]);     }     bubble_sort ( list , n );     printf ( " \n\n Time taken to complete the bubblesort %u \n " , clock () / CLOCKS_PER_SEC);     printf ( " \n The sorted list is:" );     for ( i = 0 ; i < n ; i ++)         printf ( " %d \t " , list [ i ]);     return 0 ; } void bubble_sort ( int list [], int n ) {     int temp , i , j ;     for ( i = 0 ; i < n ; i ++)     {    

C Program To Accept The String Having Length At Least Two | C Programming

Image
C Program To Accept The String Having Length At Least Two #include <stdio.h> #include <string.h> int main () {     char str [ 10 ];     int len, i, q = 0 ;     printf ( "Enter the string which only contain a and b \n " );     scanf ( " %s " , str);     len = strlen (str);     for (i = 0 ; i <= len; i++)     {         if ( str [i] == 'a' && q == 0 )             q = 1 ;         else if ( str [i] == 'b' && q == 0 )             q = 1 ;         else if ( str [i] == 'a' && q == 1 )             q = 2 ;         else if ( str [i] == 'b' && q == 1 )             q = 2 ;         else if ( str [i] == 'a' && q == 2 )             q = 3 ;         else if ( str [i] == 'b' && q == 2 )             q = 3 ;         else if ( str [i] == 'a' && q == 3 )             q = 3 ;         else if ( str [i] == 'b' && q == 3

Bubble Sort

Image
Bubble Sort Bubble sort is a very simple sorting technique. However, this sorting algorithm is not efficient in comparison to other sorting algorithms. The basic idea underlying the bubble sort is to pass through the file sequentially several times. Each pass consists of comparing each element in the file with its successor (x[i] with x[i+1]) and interchanging the two elements if they are not in proper order. Example: Consider the following file, 25          57          48          37          12          92          86          33 In first pass, following comparisons are made: x[o] with x[1] (25 with 57) No interchange x[1] with x[2] (57 with 48) Interchange x[2] with x[3] (57 with 37) Interchange x[3] with x[4] (57 with 12) Interchange x[4] with x[5] (57 with 92) No interchange x[5] with x[6] (92 with 86) Interchange x[6] with x[7] (92 with 33) Interchange Thus, after the first pass, the file is on the order: 25          48          37          

C Program To Accept The String Having Even Length | C Programming

Image
C Program To Accept The String Having Even Length #include <stdio.h> #include <string.h> int main () {     char str [ 10 ];     int len, i, q = 0 ;     printf ( "Enter the string which only contain 0 and 1 \n " );     scanf ( " %s " , str);     len = strlen (str);     for (i = 0 ; i <= len; i++)     {         if ( str [i] == '0' && q == 0 )             q = 2 ;         else if ( str [i] == '1' && q == 0 )             q = 1 ;         else if ( str [i] == '0' && q == 1 )             q = 3 ;         else if ( str [i] == '1' && q == 1 )             q = 0 ;         else if ( str [i] == '0' && q == 2 )             q = 0 ;         else if ( str [i] == '1' && q == 2 )             q = 3 ;         else if ( str [i] == '0' && q == 3 )             q = 1 ;         else if ( str [i] == '1' && q == 3 )