Saturday 4 May 2013

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

No comments: