Sunday 6 July 2014

EXAMPLE OF PALINDROME IN C PROGRAMMING

/*
The strings where the reading from the reverse and forward directions give the same word are called
a palindrome.

For example, -> the string ”radar” is an example for palindrome.
Among many other techniques stack can be used to determine if a string is a palindrome or not.
This is achieved by pushing all the letters of a given word into stack and checking if the letters popped are the same as the letter of the string.
*/

#include<stdio.h>
#include<conio.h>
#define STACKSIZE 25
typedef struct
{
int count;
int top;
char items[STACKSIZE];/*stack can contain up to 20 characters*/
}STACK;
void push(STACK *, int);
char pop(STACK *);
Sptr->count--;
}
return pp;
}
int main()
{
int i, check=0, size;
STACK s;
char p, A[20];
s.count=0;
s.top = -1; /*indicates that the stack is empty at the beginning*/
printf("Enter the string\n");
gets(A); /* alternatively you can use scanf("%s",A); */
/*pushing the characters of the string into the stack*/
for(i=0; A[i]!='\0';i++){
p = A[i];
push(&s,p);
}
size = s.count;
/*popping and checking if the letters are equal in rev. & forw.direction*/
for(i=0;i<=size-1;i++){
p=pop(&s);
if(p != A[i])
check ++;
}
if(check == 0)
printf("\nThe string %s is a palindrome\n",A);
else
printf("\nThe string %s is NOT a palindrome\n",A);
return 0;
}
void push(STACK *Sptr, int ps) /*pushes ps into stack*/
{
if(Sptr->top == STACKSIZE-1){
printf("Stack is full\n");
exit(1); /*exit from the function*/
}
else {
Sptr->top++;
Sptr->count++;
Sptr->items[Sptr->top]= ps;
}
}
char pop(STACK *Sptr)
{
char pp;
if(Sptr->top == -1){
printf("Stack is empty\n");
exit(1); /*exit from the function*/
}
else {
pp = Sptr->items[Sptr->top];
Sptr->top--;
Sptr->count--;
}
return pp;
}


Find string is a palindrome or not In c

/*
The strings where the reading from the reverse and forward directions give the same word are called
a palindrome.

For example, -> the string ”radar” is an example for palindrome.
Among many other techniques stack can be used to determine if a string is a palindrome or not.
This is achieved by pushing all the letters of a given word into stack and checking if the letters popped are the same as the letter of the string.
*/

#include<stdio.h>
#include<conio.h>
#define STACKSIZE 25
typedef struct
{
int count;
int top;
char items[STACKSIZE];/*stack can contain up to 20 characters*/
}STACK;
void push(STACK *, int);
char pop(STACK *);
Sptr->count--;
}
return pp;
}
int main()
{
int i, check=0, size;
STACK s;
char p, A[20];
s.count=0;
s.top = -1; /*indicates that the stack is empty at the beginning*/
printf("Enter the string\n");
gets(A); /* alternatively you can use scanf("%s",A); */
/*pushing the characters of the string into the stack*/
for(i=0; A[i]!='\0';i++){
p = A[i];
push(&s,p);
}
size = s.count;
/*popping and checking if the letters are equal in rev. & forw.direction*/
for(i=0;i<=size-1;i++){
p=pop(&s);
if(p != A[i])
check ++;
}
if(check == 0)
printf("\nThe string %s is a palindrome\n",A);
else
printf("\nThe string %s is NOT a palindrome\n",A);
return 0;
}
void push(STACK *Sptr, int ps) /*pushes ps into stack*/
{
if(Sptr->top == STACKSIZE-1){
printf("Stack is full\n");
exit(1); /*exit from the function*/
}
else {
Sptr->top++;
Sptr->count++;
Sptr->items[Sptr->top]= ps;
}
}
char pop(STACK *Sptr)
{
char pp;
if(Sptr->top == -1){
printf("Stack is empty\n");
exit(1); /*exit from the function*/
}
else {
pp = Sptr->items[Sptr->top];
Sptr->top--;
Sptr->count--;
}
return pp;
}

Push And Pop Data In Stack Using C Programming

/*
Stacks can be used to reverse a sequence.
For example,
->if a string ”Computers” is entered by the user the stack can be used to create and display the reverse string “sretupmoC” as follows.
The program simply pushes all of the characters of the string into the stack.
Then it pops and display until the stack is empty.
*/

#include<stdio.h>
#include<conio.h>
#define STACKSIZE 50
typedef struct{
int count;
int top;
char items[STACKSIZE];/*stack can contain up to 50 characters*/
}STACK;

void push(STACK *, char);
char pop(STACK *);

void main()
{
int i;
STACK s;
char p, A[20];
s.top = -1; /*indicates that the stack is empty at the beginning*/
s.count=0;
printf("Input the string please:\n");
gets(A); /* alternatively you can use scanf("%s",A); */
/*pushing the characters into the stack*/
for(i=0;A[i] != '\0';i++){
p = A[i];
push(&s,p);
}
printf("The string in reverse order is:");
/*popping and printing the string in reverse order*/
while(s.count >= 0){
p=pop(&s);
printf("%c",p);
}
return 0;
}

void push(STACK *Sptr, char ps) /*pushes ps into stack*/
{
if(Sptr->top == STACKSIZE-1){
printf("Stack is full\n");
Return; /*exit from the function*/
}
else {
Sptr->top++;
Sptr->count++;
Sptr->items[Sptr->top]= ps;
}
}

char pop(STACK *Sptr)
{
char pp;
if(Sptr->top == -1){
printf("\nStack is empty\n");
exit(1); /*exit from the function*/
}
else {
pp = Sptr->items[Sptr->top];
Sptr->top--;
Sptr->count--;
}
return pp;
}

Stack Using C Programming

/*
Stacks can be used to reverse a sequence.
For example,
->if a string ”Computers” is entered by the user the stack can be used to create and display the reverse string “sretupmoC” as follows.
The program simply pushes all of the characters of the string into the stack.
Then it pops and display until the stack is empty.
*/

#include<stdio.h>
#include<conio.h>
#define STACKSIZE 50
typedef struct{
int count;
int top;
char items[STACKSIZE];/*stack can contain up to 50 characters*/
}STACK;

void push(STACK *, char);
char pop(STACK *);

void main()
{
int i;
STACK s;
char p, A[20];
s.top = -1; /*indicates that the stack is empty at the beginning*/
s.count=0;
printf("Input the string please:\n");
gets(A); /* alternatively you can use scanf("%s",A); */
/*pushing the characters into the stack*/
for(i=0;A[i] != '\0';i++){
p = A[i];
push(&s,p);
}
printf("The string in reverse order is:");
/*popping and printing the string in reverse order*/
while(s.count >= 0){
p=pop(&s);
printf("%c",p);
}
return 0;
}

void push(STACK *Sptr, char ps) /*pushes ps into stack*/
{
if(Sptr->top == STACKSIZE-1){
printf("Stack is full\n");
Return; /*exit from the function*/
}
else {
Sptr->top++;
Sptr->count++;
Sptr->items[Sptr->top]= ps;
}
}

char pop(STACK *Sptr)
{
char pp;
if(Sptr->top == -1){
printf("\nStack is empty\n");
exit(1); /*exit from the function*/
}
else {
pp = Sptr->items[Sptr->top];
Sptr->top--;
Sptr->count--;
}
return pp;
}

Push And Pop characters In Stack Using C Programming

/*
Stacks can be used to reverse a sequence.
For example,
->if a string ”Computers” is entered by the user the stack can be used to create and display the reverse string “sretupmoC” as follows.
The program simply pushes all of the characters of the string into the stack.
Then it pops and display until the stack is empty.
*/

#include<stdio.h>
#include<conio.h>
#define STACKSIZE 50
typedef struct{
int count;
int top;
char items[STACKSIZE];/*stack can contain up to 50 characters*/
}STACK;

void push(STACK *, char);
char pop(STACK *);

void main()
{
int i;
STACK s;
char p, A[20];
s.top = -1; /*indicates that the stack is empty at the beginning*/
s.count=0;
printf("Input the string please:\n");
gets(A); /* alternatively you can use scanf("%s",A); */
/*pushing the characters into the stack*/
for(i=0;A[i] != '\0';i++){
p = A[i];
push(&s,p);
}
printf("The string in reverse order is:");
/*popping and printing the string in reverse order*/
while(s.count >= 0){
p=pop(&s);
printf("%c",p);
}
return 0;
}

void push(STACK *Sptr, char ps) /*pushes ps into stack*/
{
if(Sptr->top == STACKSIZE-1){
printf("Stack is full\n");
Return; /*exit from the function*/
}
else {
Sptr->top++;
Sptr->count++;
Sptr->items[Sptr->top]= ps;
}
}

char pop(STACK *Sptr)
{
char pp;
if(Sptr->top == -1){
printf("\nStack is empty\n");
exit(1); /*exit from the function*/
}
else {
pp = Sptr->items[Sptr->top];
Sptr->top--;
Sptr->count--;
}
return pp;
}

Insert And Delete Data In Stack Using C Programming

/*
Stacks can be used to reverse a sequence.
For example,
->if a string ”Computers” is entered by the user the stack can be used to create and display the reverse string “sretupmoC” as follows.
The program simply pushes all of the characters of the string into the stack.
Then it pops and display until the stack is empty.
*/

#include<stdio.h>
#include<conio.h>
#define STACKSIZE 50
typedef struct{
int count;
int top;
char items[STACKSIZE];/*stack can contain up to 50 characters*/
}STACK;

void push(STACK *, char);
char pop(STACK *);

void main()
{
int i;
STACK s;
char p, A[20];
s.top = -1; /*indicates that the stack is empty at the beginning*/
s.count=0;
printf("Input the string please:\n");
gets(A); /* alternatively you can use scanf("%s",A); */
/*pushing the characters into the stack*/
for(i=0;A[i] != '\0';i++){
p = A[i];
push(&s,p);
}
printf("The string in reverse order is:");
/*popping and printing the string in reverse order*/
while(s.count >= 0){
p=pop(&s);
printf("%c",p);
}
return 0;
}

void push(STACK *Sptr, char ps) /*pushes ps into stack*/
{
if(Sptr->top == STACKSIZE-1){
printf("Stack is full\n");
Return; /*exit from the function*/
}
else {
Sptr->top++;
Sptr->count++;
Sptr->items[Sptr->top]= ps;
}
}

char pop(STACK *Sptr)
{
char pp;
if(Sptr->top == -1){
printf("\nStack is empty\n");
exit(1); /*exit from the function*/
}
else {
pp = Sptr->items[Sptr->top];
Sptr->top--;
Sptr->count--;
}
return pp;
}

Displaying the linked list elements

/*
Function to display the student with highest CGPA in a linked list containing student data.
 Use the following node structure for your linked list.
*/
struct node{
int stNo;
float CGPA;
struct node *next;
};

typedef struct node *NODEPTR;
void DisplayMax(NODEPTR plist)
{
NODEPTR p;
float maxCGPA=-1.0;
int maxstNo;
p = plist;
/*current node*/
if(p == NULL){
printf(“no node/data is available in the list\n”);
return;
}
do{
if(p->CGPA > maxCGPA){
maxCGPA = p->CGPA;
maxstNo = p->stNo;
}
p = p->next;
} while(p!= NULL);
printf(“The student number with max CGPA: %d\n”, maxstNo);
printf(“The student’s CGPA: %d\n”, maxCGPA);
}