Thursday, 16 May 2013
Saturday, 4 May 2013
A simple program to compare to string whether they are equal or not
#include"stdio.h"
#include"conio.h"
#include"malloc.h"
int main()
{
char *firstString =(char*)malloc(100);
char *secondString =(char*)malloc(100);
void readString(char *);
void printString(char *);
int stringCompare(char *,char *);
int result;
clrscr();
printf("\nEnter First String : ");
readString(firstString);
printf("\nEnter Second String : ");
readString(secondString);
result=stringCompare(firstString,secondString);
if(result!=0)
printf("\nString are not equal");
else
printf("\nStrings are equal");
getch();
return 1;
}
//Function readString
void readString(char *s)
{
while((*s=getchar())!='\n') // Read until user press enter key
*s++;
*s=NULL;
}
//Function printString
void printString(char *s)
{
while(*s!=NULL)
putchar(*s++);
}
{
while(*first&&*second)
if(*first++!=*second++)
return 1;
return 0;
}
#include"conio.h"
#include"malloc.h"
int main()
{
char *firstString =(char*)malloc(100);
char *secondString =(char*)malloc(100);
void readString(char *);
void printString(char *);
int stringCompare(char *,char *);
int result;
clrscr();
printf("\nEnter First String : ");
readString(firstString);
printf("\nEnter Second String : ");
readString(secondString);
result=stringCompare(firstString,secondString);
if(result!=0)
printf("\nString are not equal");
else
printf("\nStrings are equal");
getch();
return 1;
}
//Function readString
void readString(char *s)
{
while((*s=getchar())!='\n') // Read until user press enter key
*s++;
*s=NULL;
}
//Function printString
void printString(char *s)
{
while(*s!=NULL)
putchar(*s++);
}
//Compare to string and returns 0 if they are equal else return 1
int stringCompare(char *first,char *second){
while(*first&&*second)
if(*first++!=*second++)
return 1;
return 0;
}
A Simple pointer example to Read String and Print the string
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
int main()
{
char *s=(char*)malloc(100); // Declaring pointer variable to char
// data type and dynamically allocating memory
void readString(char *); // Prototype for readString
void printString(char *); // Prototype for printString
clrscr();
printf("\nEnter your name : ");
readString(s);
printf("\nYour name is ");
printString(s);
getch();
return 1;
}
//Function readString
void readString(char *s)
{
while((*s=getchar())!='\n') // Read until user press enter key
*s++;
*s=NULL;
}
//Function printString
void printString(char *s)
{
while(*s!=NULL)
putchar(*s++);
}
#include"conio.h"
#include"alloc.h"
int main()
{
char *s=(char*)malloc(100); // Declaring pointer variable to char
// data type and dynamically allocating memory
void readString(char *); // Prototype for readString
void printString(char *); // Prototype for printString
clrscr();
printf("\nEnter your name : ");
readString(s);
printf("\nYour name is ");
printString(s);
getch();
return 1;
}
//Function readString
void readString(char *s)
{
while((*s=getchar())!='\n') // Read until user press enter key
*s++;
*s=NULL;
}
//Function printString
void printString(char *s)
{
while(*s!=NULL)
putchar(*s++);
}
How to print the nth number of a Fibonacci series by recursive method in C programming
#include"stdio.h"
#include"conio.h"
void main()
{
int num;
long fibo(int);
void printFiboSeries(int);
clrscr();
printf("\nEnter number : ");
scanf("%d",&num);
printFiboSeries(num);
getch();
}
long fibo(int n)
{
if(n<=2)
return 1;
return fibo(n-1) + fibo(n-2);
}
void printFiboSeries(int n)
{
if(n<1 br="">return;
printFiboSeries(n-1);
printf("%d ",fibo(n)); // printf is called when all function call of printFiboSeries is popped up from
// the stack
}
Suppose input variable is 5 then the result should be 5
fibo(5)
fibo(4) + f ibo(3)
fibo(3) + f ibo(2) + fibo(2) + fibo(1)
fibo(2) + fibo(1) + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 = 5
Subscribe to:
Posts (Atom)