Showing posts with label Basics. Show all posts
Showing posts with label Basics. Show all posts

Sunday, June 18, 2017

Positions of substring in the String

#include <stdio.h>
#include <string.h>

char str[100], sub[100];
int count = 0, count1 = 0;
int position[100];

void main()
{
    int i, j, l, l1, l2;

    printf("\nEnter a string : ");
    scanf("%[^\n]s", str);

    l1 = strlen(str);

    printf("\nEnter a substring : ");
    scanf(" %[^\n]s", sub);

    l2 = strlen(sub);

    for (i = 0; i < l1;)
    {
        j = 0;
        int temp = i;
        count = 0;
        while ((str[i] == sub[j]))
        {
            count++;
            i++;
            j++;
        }
        if (count == l2)
        {
            position[count1] = temp;
            count1++;                                  
            count = 0;
            i = temp + 1;
        }
        else
            i = temp + 1;
    }
   
    printf("%s occurs %d times in %s", sub, count1, str);
    int x;
    for(x=0;x< 100;x++)
    {
        if(position[x]==0){
            break;}
       
        printf("\n %d \t", position[x]);
    }
}

Thursday, February 13, 2014

Static variable example in C

/*  
  * static_c.c  
  *   
  * Copyright 2014 Afiz   
  *   
  *   
  */  
 #include <stdio.h>  
 void statfunc();   
 int main(int argc, char **argv)  
 {  
      int i=0;   
      for(i=0;i<10;i++)  
           statfunc();   
      return 0;  
 }  
 void statfunc()  
 {  
      static int stat_var=0;   
      int local_var =0;   
      printf("static valuce = %d\t Normal Variable =%d\n",stat_var, local_var);  
      stat_var++;   
      local_var++;   
 }  

Sunday, March 10, 2013

C format specifiers


Format Specifiers in C
%i or %d int
%c char
%f float
%lf double
%s string
%hi short
%ld long int
%d,%hu unsinged short
%u unsinged int


  • \n (newline)
  • \t (tab)
  • \v (vertical tab)
  • \f (new page)
  • \b (backspace)
  • \r (carriage return)

Format Specifier for int and short in C example

 /*  
  * FormatSpecifiers.c  
  * Copyright 2013 Afiz <afiz@afiz-Extensa-4620>  
  */  
 #include <stdio.h>  
 int main(int argc, char **argv)  
 {  
      int a =109990909;   
      short b = 23;  
      printf("%d,%hi\n",a,b);  
      return 0;  
 }  

Monday, March 12, 2012

Facts about Sorting Techniques

 Bubble sort: comparisons O(n2), Swaps O(n2)  
 Selection Sort: Comparisons O(n2), swaps O(n)  
 Insertion Sort: Comparisons O(n2) , no swaps   
 Insertion sort is best basic sorts.  

Selection Sort example program in C

 /*  
 This is a basic program of Selection sort.  
 Author: Afiz  
 Date:  
 */  
 #include<stdio.h>  
 main()  
 {  
 int a[5]={9,7,4,8,1};// array with 5 elements.  
 int out,in,min,i;// local variable declarations   
 for(out =0;out<4;out++)// outer loop   
      {  
           min =out;   
      for(in=out+1;in<5;in++)// inner loop   
           {  
                if(a[in]<=a[min])  
                     min=in;   
           }  
      int tmp = a[min]; // swap ....   
      a[min]=a[out];  
      a[out]=tmp;  
      }  
 //printing array   
 for(i=0;i<5;i++)  
 {  
      printf("%d\n",a[i]);   
 }  
 }  

Insertion Sort Example program in C

 /*  
 This is a basic program of Insertion sort.  
 Author: Afiz  
 Date:  
 */  
 #include<stdio.h>  
 main()  
 {  
 int a[5]={9,7,4,8,1};// array with 5 elements.  
 int out,in,min,i;// local variable declarations   
 for(out=1;out<5;out++) // outer loop   
      {  
           temp = a[out];  
           in =out;   
      while(in>0 && a[in-1]>=temp) // inner loop   
           {  
                a[in]=a[in-1];  
                --in;   
           }  
           a[in]=temp;   
      }  
 //printing array ..   
 for(i=0;i<5;i++)  
      {  
           printf("%d\n",a[i]); //   
      }  
 }  

Bubble Sort example program in C

 /*  
 This is a basic program of Bubble sort.  
 Author: Afiz  
 Date:  
 */  
 #include<stdio.h>  
 main()  
 {  
 int a[5]={5,5,1,7,-1}; // array with 5 elements.  
 int i,j; // local variable declarations   
 for(i=0;i<5;i++)// outer loop   
 {  
      for(j=1;j<5-i;j++) // outter loop   
           {  
                if(a[j-1]>=a[j])  
                {  
                     int tmp = a[j-1]; // swap   
                     a[j-1]=a[j];  
                     a[j]=tmp;       
                }  
           }  
 }  
 //printing array   
 for(i=0;i<5;i++)  
 {  
      printf("%d\n",a[i]);   
 }  
 }