C Program of reversing a string using stack


 

/*Program of reversing a string using stack */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAX 20

int top = -1;
char stack[MAX];
char pop();
void push(char);

main()
{
char str[20];
unsigned int i;
printf(“Enter the string : ” );
gets(str);
/*Push characters of the string str on the stack */
for(i=0;i<strlen(str);i++)
push(str[i]);
/*Pop characters from the stack and store in string str */
for(i=0;i
str[i]=pop();
printf(“Reversed string is : “);
puts(str);
}/*End of main()*/

void push(char item)
{
if(top == (MAX-1))
{
printf(“Stack Overflow\n”);
return;
}
stack[++top] =item;
}/*End of push()*/

char pop()
{
if(top == -1)
{
printf(“Stack Underflow\n”);
exit(1);
}
return stack[top–];
}/*End of pop()*/

 

Program to transpose a sparse matrix using Linked Lists


 

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

#define MAX1 3
#define MAX2 3

/* structure for col headnode */
struct cheadnode
{
int colno ;
struct node *down ;
struct cheadnode *next ;
} ;

/* structure for row headnode */
struct rheadnode
{
int rowno ;
struct node * right ;
struct rheadnode *next ;
} ;

/* structure for node to store element */
struct node
{
int row ;
int col ;
int val ;
struct node *right ;
struct node *down ;
} ;

/* structure for special headnode */
struct spmat
{
struct rheadnode *firstrow ;
struct cheadnode *firstcol ;
int noofrows ;
int noofcols ;
} ;

struct sparse
{
int *sp ;
int row ;
struct spmat *smat ;
struct cheadnode *chead[MAX2] ;
struct rheadnode *rhead[MAX1] ;
struct node *nd ;
} ;

void initsparse ( struct sparse * ) ;
void create_array ( struct sparse * ) ;
void display ( struct sparse ) ;
int count ( struct sparse ) ;
void create_triplet ( struct sparse *, struct sparse ) ;
void create_llist ( struct sparse * ) ;
void insert ( struct sparse *, struct spmat *, int, int, int ) ;
void show_llist ( struct sparse ) ;
void delsparse ( struct sparse * ) ;

void main( )
{
struct sparse s1, s2 ;

clrscr( ) ;

initsparse ( &s1 ) ;
initsparse ( &s2 ) ;

create_array ( &s1 ) ;

printf ( “\nElements in sparse matrix: ” ) ;
display ( s1 ) ;

create_triplet ( &s2, s1 ) ;

create_llist ( &s2 ) ;
printf ( “\n\nInformation stored in linked list : ” ) ;
show_llist ( s2 ) ;

delsparse ( &s1 ) ;
delsparse ( &s2 ) ;

getch( ) ;
}

/* initializes structure elements */
void initsparse ( struct sparse *p )
{
int i ;
/* create row headnodes */
for ( i = 0 ; i < MAX1 ; i++ )
p -> rhead[i] = ( struct rheadnode * ) malloc ( sizeof ( struct rheadnode ) ) ;

/* initialize and link row headnodes together */
for ( i = 0 ; i < MAX1 – 1 ; i++ )
{
p -> rhead[i] -> next = p -> rhead[i + 1] ;
p -> rhead[i] -> right = NULL ;
p -> rhead[i] -> rowno = i ;
}
p -> rhead[i] -> right = NULL ;
p -> rhead[i] -> next = NULL ;

/* create col headnodes */
for ( i = 0 ; i < MAX1 ; i++ )
p -> chead[i] = ( struct cheadnode * ) malloc ( sizeof ( struct cheadnode ) ) ;

/* initialize and link col headnodes together */
for ( i = 0 ; i < MAX2 – 1 ; i++ )
{
p -> chead[i] -> next = p -> chead[i + 1] ;
p -> chead[i] -> down = NULL ;
p -> chead[i] -> colno = i ;
}
p -> chead[i] -> down = NULL ;
p -> chead[i] -> next = NULL ;

/* create and initialize special headnode */

p -> smat = ( struct spmat * ) malloc ( sizeof ( struct spmat ) ) ;
p -> smat -> firstcol = p -> chead[0] ;
p -> smat -> firstrow = p -> rhead[0] ;
p -> smat -> noofcols = MAX2 ;
p -> smat -> noofrows = MAX1 ;
}

/* creates, dynamically the matrix of size MAX1 x MAX2 */
void create_array ( struct sparse *p )
{
int n, i ;

p -> sp = ( int * ) malloc ( MAX1 * MAX2 * sizeof ( int ) ) ;

/* get the element and store it */
for ( i = 0 ; i < MAX1 * MAX2 ; i++ )
{
printf ( “Enter element no. %d:”, i ) ;
scanf ( “%d”, &n ) ;
* ( p -> sp + i ) = n ;
}
}

/* displays the contents of the matrix */
void display ( struct sparse s )
{
int i ;

/* traverses the entire matrix */
for ( i = 0 ; i < MAX1 * MAX2 ; i++ )
{
/* positions the cursor to the new line for every new row */
if ( i % MAX2 == 0 )
printf ( “\n” ) ;
printf ( “%d\t”, * ( s.sp + i ) ) ;
}
}

/* counts the number of non-zero elements */
int count ( struct sparse s )
{
int cnt = 0, i ;

for ( i = 0 ; i < MAX1 * MAX2 ; i++ )
{
if ( * ( s.sp + i ) != 0 )
cnt++ ;
}
return cnt ;
}

/* creates an array of triplet containing info. about non-zero elements */
void create_triplet ( struct sparse *p, struct sparse s )
{
int r = 0 , c = -1, l = -1, i ;

p -> row = count ( s ) ;
p -> sp = ( int * ) malloc ( p -> row * 3 * sizeof ( int ) ) ;

for ( i = 0 ; i < MAX1 * MAX2 ; i++ )
{
c++ ;
/* sets the row and column values */
if ( ( ( i % MAX2 ) == 0 ) && ( i != 0 ) )
{
r++ ;
c = 0 ;
}

/* checks for non-zero element. Row, column and
non-zero element value is assigned to the matrix */
if ( * ( s.sp + i ) != 0 )
{
l++ ;
* ( p -> sp + l ) = r ;
l++ ;
* ( p -> sp + l ) = c ;
l++ ;
* ( p -> sp + l ) = * ( s.sp + i ) ;
}
}
}

/* stores information of triplet in a linked list form */
void create_llist ( struct sparse *p )
{
int j = 0, i ;
for ( i = 0 ; i < p -> row ; i++, j+= 3 )
insert ( p, p -> smat, * ( p -> sp + j ), * ( p -> sp + j + 1 ),
* ( p -> sp + j + 2) ) ;
}

/* inserts element to the list */
void insert ( struct sparse *p, struct spmat *smat , int r, int c, int v )
{
struct node *temp1, *temp2 ;
struct rheadnode *rh ;
struct cheadnode *ch ;
int i, j ;

/* allocate and initialize memory for the node */

p -> nd = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
p -> nd -> col = c ;
p -> nd -> row = r ;
p -> nd -> val = v ;

/* get the first row headnode */

rh = smat -> firstrow ;

/* get the proper row headnode */
for ( i = 0 ; i < r ; i++ )
rh = rh -> next ;
temp1 = rh -> right ;

/* if no element added in a row */
if ( temp1 == NULL )
{
rh -> right = p -> nd ;
p -> nd -> right = NULL ;
}
else
{
/* add element at proper position */
while ( ( temp1 != NULL ) && ( temp1 -> col < c ) )
{
temp2 = temp1 ;
temp1 = temp1 -> right ;
}
temp2 -> right = p -> nd ;
p -> nd -> right = NULL ;
}

/* link proper col headnode with the node */

ch = p -> smat -> firstcol ;
for ( j = 0 ; j < c ; j++ )
ch = ch -> next ;
temp1 = ch -> down ;

/* if col not pointing to any node */
if ( temp1 == NULL )
{
ch -> down = p -> nd ;
p -> nd -> down = NULL ;
}
else
{
/* link previous node in column with next node in same column */
while ( ( temp1 != NULL ) && ( temp1 -> row < r ) )
{
temp2 = temp1 ;
temp1 = temp1 -> down ;
}
temp2 -> down = p -> nd ;
p -> nd -> down = NULL ;
}
}

void show_llist ( struct sparse s )
{
struct node *temp ;
/* get the first row headnode */
int r = s.smat -> noofrows ;
int i ;

printf ( “\n” ) ;

for ( i = 0 ; i < r ; i++ )
{
temp = s.rhead[i] -> right ;
if ( temp != NULL )
{
while ( temp -> right != NULL )
{
printf ( “Row: %d Col: %d Val: %d\n”, temp -> row, temp -> col,
temp -> val ) ;
temp = temp -> right ;
}
if ( temp -> row == i )
printf ( “Row: %d Col: %d Val: %d\n”, temp -> row, temp -> col,
temp -> val ) ;
}
}
}

/* deallocates memory */
void delsparse ( struct sparse *p )
{
int r = p -> smat -> noofrows ;
struct rheadnode *rh ;
struct node *temp1, *temp2 ;
int i, c ;

/* deallocate memeory of nodes by traversing rowwise */
for ( i = r – 1 ; i >= 0 ; i– )
{
rh = p -> rhead[i] ;
temp1 = rh -> right ;

while ( temp1 != NULL )
{
temp2 = temp1 -> right ;
free ( temp1 ) ;
temp1 = temp2 ;
}
}

/* deallocate memory of row headnodes */
for ( i = r – 1 ; i >= 0 ; i– )
free ( p -> rhead[i] ) ;

/* deallocate memory of col headnodes */

c = p -> smat -> noofcols ;
for ( i = c – 1 ; i >= 0 ; i– )
free ( p -> chead[i] ) ;
}

\n

Transposing a Sparse Matrix Using Array [Part 1-Long]


 

#include<stdio.h>
#include<conio.h>
// transpose for the sparse matrix
main()
{
//clrscr();
int a[10][10],b[10][10];
int m,n,p,q,t,col;
int i,j;

printf(“enter the no of row and columns :\n”);
scanf(“%d %d”,&m,&n);

// assigning the value of matrix

for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
printf(“a[%d][%d]= “,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“\n\n”);

//displaying the matrix

printf(“\n\nThe matrix is :\n\n”);

for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
printf(“%d”,a[i][j]);
}
printf(“\n”);
}
t=0;

printf(“\n\nthe non zero value matrix are :\n\n”);

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

// accepting only non zero value
if(a[i][j]!=0)
{
t=t+1;
b[t][1]=i;
b[t][2]=j;
b[t][3]=a[i][j];
} }
}

// displaying the matrix of non-zero value

printf(“\n”);
printf(“a[0 %d %d %d\n”,m,n,t);

for(i=1;i<=t;i++)
{
printf(“a[%d %d %d %d \n”,i,b[i][1],b[i][2],b[i][3]);
}

b[0][1]=n; b[0][2]=m; b[0][3]=t;
q=1;

// transpose of the matrix

printf(“\n\nthe transpose of the matrix :\n “);

if(t>0)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=t;j++)
{
if(b[j][2]==i)
{
a[q][1]=b[j][2]; a[q][2]=b[j][1];
a[q][3]=b[j][3]; q=q+1;
} }
} }

printf(“\n\n”);
printf(“a[0 %d %d %d\n”,b[0][1],b[0][2],b[0][3]);

for(i=1;i<=t;i++)
{
printf(“a[%d %d %d %d\n”,i,a[i][1],a[i][2],a[i][3]);
}
getch();
}

\n

String Pattern Matching in C using pointers


 

Block diagrams of (Left) pattern matching and ...
Block diagrams of (Left) pattern matching and substitution method and (Right) soft pattern matching method. (Photo credit: Wikipedia)

 

String Pattern Matching Program in C using Pointers

 

Its an application of Arrays

 

 

#include<stdio.h>
#include<conio.h>
int match(char*, char*);

main()
{
char a[100], b[100];
int position;

printf(“Enter some text\n”);
gets(a);

printf(“Enter a string to find\n”);
gets(b);

position = match(a, b);

if(position!=-1)
printf(“Found at location %d\n”, position+1);
else
printf(“Not found.\n”);

getch();
}

int match(char *a, char *b)
{
int c;
int position = 0;
char *x, *y;

x = a;
y = b;

while(*a)
{
while(*x==*y)
{
x++;
y++;
if(*x==”||*y==”)
break;
}
if(*y==”)
break;

a++;
position++;
x = a;
y = b;
}
if(*a)
return position;
else
return -1;
}

 

 

 

\n

 

Array Complete Program using C[DATA STRUCTURE]


 

Real (Laplace) spherical harmonics for to (top...
Real (Laplace) spherical harmonics for to (top to bottom) and to (left to right). The negative order harmonics are rotated about the axis by with respect to the positive order ones. (Photo credit: Wikipedia)

 

Here is what I present Before you is a complete array program where all operations of arrays are included eg:-

 

  1. Insertion

  2. Deletion

  3. Searching

  4. Sorting

  5. Dispay

 

#include <stdio.h>
#include <conio.h>
int n; //size of array

int BSearch(int *a,int ITEM)
{
int beg=0,end=n-1;
int mid;

while( beg<=end )
{
mid=(beg+end)/2;
if(a[mid]==ITEM)
return mid;
else if( a[mid] < ITEM )
beg=mid+1;
else
end=mid-1;
}

return -1;
}

void SSort(int *a)
{
int i,j,temp;

for(i=0; i
{
for(j=i+1; j
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

}

void DEL(int *a, int ITEM)
{
int i;
int pos=BSearch(a, ITEM);

if(pos!=-1)
{
for(i=pos; i
a[i]=a[i+1];
n–;
printf(“\n Item Deleted. “);
}
else
printf(“\n Item not found. “);
}

void INS(int *a, int ITEM)
{
int pos,i;

//Finding the proper position for Sorted Array
if(ITEM>a[n-1])
pos=n;
else if(ITEM<a[0])
pos=0;
else
{
for(i=0; i
if(a[i]<=ITEM && a[i+1]>=ITEM)
{
pos=i+1;
break;
}
}

//Shifting of elements to Insert the element.
for(i=n; i>pos; i–)
a[i]=a[i-1];

a[pos]=ITEM;
n++;
printf(“\n Item Inserted. “);
}

void Display(int *a)
{
int i;
for(i=0; i<n; i++)
printf(” %d”,a[i]);
}

int main()
{
int a[100],ch,pos,ITEM,i;

printf(“\n Enter the number of Elements: “);
scanf(“%d”,&n);

printf(“\n Enter the Elements: “);
for(i=0; i<n; i++)
scanf(“%d”,&a[i]);

do
{
printf(“\n ______________________________________ “);
printf(“\n MENU:\n”);
printf(“\n 1. Insert an element. “);
printf(“\n 2. Delete an element. “);
printf(“\n 3. Search for an element. “);
printf(“\n 4. Display elements. “);
printf(“\n 0. Exit. “);
printf(“\n Your Choice: “);
scanf(“%d”,&ch);

switch(ch)
{

case 1: //INSERT
printf(“\n Enter the Item to Insert: “);
scanf(“%d”,&ITEM);

SSort(a);
INS(a,ITEM);
Display(a);

break;

case 2: //DELETE
printf(“\n Enter the Item to Delete: “);
scanf(“%d”,&ITEM);

SSort(a);
DEL(a,ITEM);
Display(a);

break;

case 3: //SEARCH
printf(“\n Enter the Item to Search: “);
scanf(“%d”,&ITEM);

SSort(a);
pos=BSearch(a,ITEM);

if(pos==-1)
printf(“\n Item not Found. “);
else
printf(“\n Item found at %d position. “,pos+1);

break;

case 4: //DISPLAY
printf(“\n The Elements are: “);

SSort(a);
Display(a);

break;

case 0:
printf(“\n Exiting… “);
break;
}

}while(ch!=0);
getch();
}

 

 

String concatenation in c without using strcat Function


#include
#include

void stringConcat(char[],char[]);
int main()
{
char strl[100],str2[100];
int compare;
clrscr();
printf(“\nEnter first string: \n”);
scanf(“%s”,strl);
printf(“\nEnter second string: “);
scanf(“%s”,str2);
stringConcat(strl,str2);
printf(“\nString after concatenation: %s”,strl);
return 0;
getch();
}

void stringConcat(char strl[],char str2[])
{
int i=0,j=0;
while(strl[i]!=”)
{
i++;
}
while(str2[j] ! = ”)
{
strl[i] = str2[j] ;
i++;
j++;
}

strl[i] = ‘ ‘;
}