Showing posts with label Pointer. Show all posts
Showing posts with label Pointer. Show all posts

Thursday, July 21, 2011

Program to count no of characters, blank spaces, lines in a file

/*Serial No.104     [swami81.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int i;
char ch,c=0,nl=1,name[20],bs=0;
clrscr();
FILE *fp1;
printf("Enter the name of file=%s\n",name);
gets(name);       //name is FILE NAME.
fp1=fopen(name,"r");
if(fp1==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp1);
    exit;
    }
printf("file data are as follows:\n\n");
while(1)
    {
    ch=fgetc(fp1);
    if(ch==EOF)
       break;
    else
       c++;          // c is no. of characters.
       if(ch=='\n')
         {
         nl++;       // nl is no. of lines.
         }
       if(ch==' ')
         {
         bs++;       // bs is no. of blank spaces.
         }
    }
printf("No of characters in file=%d\n\n",c);
printf("No of blank spaces in file=%d\n\n",bs-1);
printf("No of characters in file includes blank spaces,tabs,new line characters,\nevery charater\n\n");
printf("No of lines in file=%d\n",nl);
fclose(fp1);
getch();
}

Wednesday, July 20, 2011

Program to reverse the string {using Pointer}

/*Serial No.55     [swami27.cpp]*/

#include<stdio.h>
#include<conio.h>
void reverse(char *);
void main()
{
clrscr();
  char st[10];
  gets(st);
  printf("reverse=");
  reverse(st);
  printf("\noriginal data=%s",st);
  getch();
}
void reverse(char *s)
{
  char c;
  if (*s==NULL)
  {
     return;
  }
  c=*s;
  s++;
  reverse(s);
  printf("%c",c);
}

Tuesday, July 19, 2011

Program to concanate two strings using pointer and function by passing strings as parameter

/*Serial No.120     [swami97.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void concat(char *t, char *p);
void main()
{
char str1[10],str2[10];
int l;
clrscr();
printf("Enter the string==>");
gets(str1);
printf("Enter the string==>");
gets(str2);
concat(str1,str2);
getch();
}
void concat(char *s1, char *s2)
    {
    char new_str[20];
    int i=0;
    while(*s1)
        {
        new_str[i]=*s1;
        *s1++;
        i++;
        }
    while(*s2)
        {
        new_str[i]=*s2;
        s2++;
        i++;
        }
    new_str[i]='\0';
    puts(new_str);
    }

Program to understand use of pointers in MATRIX

/*Serial No.126     [swami105.cpp]*/

/*Accessing matrix elements & their address using pointer*/
#include<stdio.h>
#include<conio.h>
int i,j;
void main()
{
int mat1[3][3]={{11,12,13},{14,15,16},{17,18,19}};
clrscr();
printf("First Entered matrix is:\n");
for(i=0;i<3;i++)
    {
    printf("\n");
    for(j=0;j<3;j++)
        {
        printf("\t%d",mat1[i][j]);
        }
    }
printf("\n\n Address of first row  =%u",mat1);
printf("\n Address of Second row =%u",mat1+1);
printf("\n Address of Third row  =%u\n",mat1+2);
printf("\n Address of\t(0,0) =%u",*(mat1+0)+0);
printf("\t (0,1) =%u",*(mat1+0)+1);
printf("\t (0,2) =%u",*(mat1+0)+2);
printf("\n Address of\t(1,0) =%u",*(mat1+1)+0);
printf("\t (1,1) =%u",*(mat1+1)+1);
printf("\t (1,2) =%u",*(mat1+1)+2);
printf("\n Address of\t(2,0) =%u",*(mat1+2)+0);
printf("\t (2,1) =%u",*(mat1+2)+1);
printf("\t (2,2) =%u",*(mat1+2)+2);

printf("\n\n Value at\t(0,0) =%d",*(*(mat1+0)+0));
printf("\t (0,1) =%d",*(*(mat1+0)+1));
printf("\t (0,2) =%d",*(*(mat1+0)+2));
printf("\n Value at\t(1,0) =%d",*(*(mat1+1)+0));
printf("\t (1,1) =%d",*(*(mat1+1)+1));
printf("\t (1,2) =%d",*(*(mat1+1)+2));
printf("\n Value at\t(2,0) =%d",*(*(mat1+2)+0));
printf("\t (2,1) =%d",*(*(mat1+2)+1));
printf("\t (2,2) =%d",*(*(mat1+2)+2));

getch();
}

Program for stack using Array & pointer {push, pop, display}

/*Serial No.153     [swami135.cpp]*/

#include<stdio.h>
#include<conio.h>
#define max 5

void push(int *S, int *top)
    {
    int ele;
    if(*top<max-1)
        {
        (*top)++;
        printf("Enter the element to insert= ");
        scanf("%d",&ele);
        *(S+*top)=ele;
        }
    else
        printf("\nStack is full.\n");
    }

void display_stack(int *S, int top)
    {
    printf("\nElements in the stack are:\n");
    if(top==-1)
        printf("\nStack is empty.\n");
    else
        {
        for(int i=0;i<=top;i++)
            {
            printf("%d ",*(S+i));
            }
        }
    }

void pop(int *S, int *top)
    {
    if(*top==-1)
        printf("\nStack is Empty.You can't pop.\n");
    else
        {
        int value=*(S+*top);
        (*top)--;
        printf("\nElement poped= %d\n",value);
        }
    }

void main()
{
int choice,tos=-1,stack[max];
clrscr();
do
    {
    printf("\n\t[1] Push");
    printf("\n\t[2] Pop");
    printf("\n\t[3] Display");
    printf("\n\t[7] Exit");
    printf("\nEnter your choice= ");
    scanf("%d",&choice);
    switch(choice)
        {
        case 1: push(stack,&tos);
            break;
        case 2: pop(stack,&tos);
            break;
        case 3: display_stack(stack,tos);
        }
    }while(choice!=7);
getch();
}