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++;   
 }  

Tuesday, October 1, 2013

Linked List Program in C


/*
LinkedList.c: this program is implementation of LinkedList in C. 

Date: 02/10/2013. 

*/


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

void CreateNode(); 
void InsertElements(); 
void DisplayElements();
void DeleteElement(int a);

typedef struct 
{
 int data; 
 struct Node *next; 
}Node;

typedef Node *List;
List head,tail,temp; 

int main()
{
 int choice,data;  
 temp =NULL; head =NULL ; tail =head; 
 while(1){
 printf("==Linked List Menu==\n1.Insert Element\n2.Display\n3.Delete\n4.Exit\n");
 scanf("%d",&choice); 
 switch(choice)
 {
 case 1:
  InsertElements(); 
  break; 
 case 2:
  DisplayElements();
  break;
 case 3:
  printf("Enter the number that you want to delete\n");  
  scanf("%d",&data);
  DeleteElement(data); break; 
 default:
  printf("Thanks for using My programming ..\n");
  exit(0); 
 }
 }// end of infinite loop  


 return 0; 
}

void CreateNode()
{

 int data; 
 printf("Enter your element\n"); 
 scanf("%d",&data);
 temp = (List)malloc(sizeof(Node)); 
 temp->data = data; 
 temp->next =NULL; 
 
} 
void InsertElements()
{
 CreateNode(); 
 if(head == NULL)
 {
  head = temp; 
  tail =head; 
 }
 else 
 {
  tail->next = temp; 
  tail = tail->next;  
 }
}
void DisplayElements()
{
 if(head!=NULL)
 {
  printf("Elements===>\n");
 for(temp =head; temp!= NULL ; temp = temp->next)
  {
   printf("%d\t",temp->data);
  }
  printf("\n");
 }
 else
 {
  printf("Your Linked List Empty\n");
 }
}

void DeleteElement(int a)
{
 if(head!=NULL)
 {
  if(head->data == a)
  {
   head = head->next;
  }
  else{
   tail = head; 
   temp = head->next; 
   int found =0; 
  while(temp !=NULL)
   {
    if(temp->data ==a)
     {
     tail->next = temp->next;
 
          printf("element is found\\deleted\nPlease use option 2 to see remaining");
     found =1;     
     break;  
            }  
     else
     {
      tail = temp; 
      temp = temp->next;  
            }   
   }
   if(!found)
   printf("element is not present\n");   
      
      }
  
 
 }
 else
 {
  printf("Your Linked List Empty\n");
 }
}

Monday, March 11, 2013

SORTING ARRAY OF 0 AND 1 WITH ONE LOOP example in C


source code:

#include <stdio.h>

int main(int argc, char **argv)
{
   
    int a[]={1,1,1,1,0,0,0,0,1,1};  
      int j=0,i; 
      for(i=0;i<10;i++) 
      { 
           if(a[i]==0) 
           { 
             a[i]=1; 
            a[j++]=0;                
           } 
      } 
     for(i=0;i<10;i++)
      printf("%d\n",a[i]);
    return 0;
}

Sunday, March 10, 2013

Functions in C examples

 A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation. With properly designed functions, it is possible to ignore how a job is done; knowing what is done is sufficient. C makes the sue of functions easy, convenient and efficient; you will often see a short function defined and called only once, just because it clarifies some piece of code.


 A function definition Syntax:   
 return-type function-name(parameter declarations, if any)  
 {  
   declarations  
   statements  
 }  
 Example:   
 /*  
 power.c: This program will give you brief idea about functions.   
 Author: Afiz S   
 Date: 11/08/10  
 */  
 #include  
 int power(int a, int b); //prototype of the function.   
 main()  
 {  
 int a,b;  
 printf("Enter your 2 number base and exponent\n");  
 scanf("%d%d",&a,&b);  
 printf("%dpower %d is = %d\n",a,b,power(a,b)); // calling function.   
 }  
 int power(int a, int b) // function defination   
 {  
 int i,base=a;  
 for(i=1;i  
  {  
  //printf("%d\n",a);  
  a=a*base;  
  }  
  return a;  
 }