Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Wednesday, 22 January 2014

Program finding number of words, blank spaces, special symbols, digits, vowels using pointers


Count Number of Words using Pointer

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<conio.h>

/*low implies that position of pointer is within a word*/
#define low 1 
/*high implies that position of pointer is out of word.*/
#define high 0 

void main()
{
int nob,now,nod,nov,nos,pos=high;
char *s;
nob=now=nod=nov=nos=0;
clrscr();

printf("Enter any string:");
gets(s);

while(*s!='')
{
if(*s==' ')  /* counting number of blank spaces. */
    {
    pos=high;
    ++nob;
    }
else if(pos==high) /* counting number of words. */
    {
    pos=low;
    ++now;
    }

if(isdigit(*s))   /* counting number of digits. */
    ++nod;
if(isalpha(*s))    /* counting number of vowels */
    switch(*s)
        {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U':
        ++nov;
        break;
        }

/* counting number of special characters */
if(!isdigit(*s)&&!isalpha(*s))
    ++nos;

s++;
}

printf("nNumber of words %d",now);
printf("nNumber of spaces %d",nob);
printf("nNumber of vowels %d",nov);
printf("nNumber of digits %d",nod);
printf("nNumber of special characters %d",nos);

getch();
}

Output :

Enter any string : pritesh a taral from c4learn.com

Number of words 5
Number of spaces 4
Number of vowels 9
Number of digits 1
Number of special characters 5

Explanation of Program :

Now we are going to accept string using gets(). We are not using scanf() to accept string because scanf() will accept string only upto whitespace.
printf("Enter any string:");
gets(s);
We are accepting string using character pointer. Now we are checking each character using character pointer and in each loop we are incrementing the character pointer.
while(*s!='')
{
------
------
}
Whenever first space is encountered then number of space counter is incremented by one.
if(*s==' ')
    {
    pos=high;
    ++nob;
    }
else if(pos==high)
    {
    pos=low;
    ++now;
    }
In the if block we are checking that whether our pointer position is within the word or outside the word.

Arithmetic operation with pointer in c programming




Rule 1: Addition arithmetic with pointers

Address + Number= Address
Address - Number= Address

Address++ = Address
Address-- = Address

++Address = Address
--Address = Address

If we will add or subtract a number from an address result will also be an address.
New address will be:





(1)What will be output of following c program?


#include<stdio.h>

int main(){

int *ptr=( int *)1000;

ptr=ptr+3;
printf(" %u",ptr);


return 0;
}



Output: 1006



(2)What will be output of following c program?


#include<stdio.h>

int main(){

double *p=(double *)1000;

p=p+3;
printf(" %u",p);


return 0;
}

Output: 1024



(3)What will be output of following c program?


#include<stdio.h>

int main(){

float array[5]={1.1f,2.2f,3.3f};
float(*ptr)[5];

ptr=&array;
printf("%u",ptr);

ptr=ptr+1;
printf(" %u",ptr);


return 0;
}

Output: 1000 1020

Rule 2: Difference arithmetic with pointers

Address - Address=Number

If you will subtract two pointers result will be a number but number will not simple mathematical subtraction of two addresses but it follow following rule:
If two pointers are of same type then:





Consider following example:


#include<stdio.h>

int main(){

int *p=(int *)1000;
int *temp;

temp=p;
p=p+2;

printf("%u %u\n",temp,p);
printf("difference= %d",p-temp);


return 0;
}

Output: 1000 1004
Difference= 2

Explanation:
Here two pointer p and temp are of same type and both are pointing to int data type varaible.
p-temp = (1004-1000)/sizeof(int)
=4/2
=2





(1)What will be output of following c program?


#include<stdio.h>

int main(){

float *p=(float *)1000;
float *q=(float *)2000;

printf("Difference= %d",q-p);


return 0;
}

Output: Difference= 250

Explanation:
q-p=(2000-100)/sizeof(float)
=1000/4
=250

Rule 3: Illegal arithmetic with pointers

Address + Address=Illegal
Address * Address=Illegal
Address / Address=Illegal
Address % Address=Illegal

What will be output of following c program?


#include<stdio.h>

int main(){

int i=5;
int *p=&i;
int *q=(int *)2;

printf("%d",p+q);


return 0;
}

Output: Compiler error

Rule 4: We can use relation operator and condition operator between two pointers.

a. If two pointers are near pointer it will compare only its offset address.



What will be output of following c program?


#include<stdio.h>

int main(){

int near*p=(int near*)0x0A0005555;
int near*q=(int near*)0x0A2115555;

if(p==q)
printf("Equql");
else
printf("Not equal");

    return 0;
}

Output: Equal





b. If two pointers are far pointer it will compare both offset and segment address.

What will be output of following c program?


#include<stdio.h>

int main(){

int far*p=(int far*)0x0A0005555;
int far*q=(int far*)0x0A2115555;

if(p==q)
printf("Equql");
else
printf("Not equal");

    return 0;
}

Output: Not equal



c. If two pointers are huge pointer it will first normalize into the 20 bit actual physical address and compare to its physical address.



What will be output of following c program?


#include<stdio.h>

int main(){

int huge*p=(int huge*)0x0A0005555;
int huge*q=(int huge*)0x0A2113445;

if(p==q)
printf("Equql");
else
printf("Not equal");

    return 0;
}

Output: Equal



Rule 5: Bit wise arithmetic with pointers


We can perform bit wise operation between two pointers like

Address & Address=Illegal
Address | Address=Illegal
Address ^ Address=Illegal
~Address=Illegal





What will be output of following c program?


#include<stdio.h>

int main(){

int i=5,j=10;
int *p=&i;
int *q=&j;

printf("%d",p|q);


return 0;
}

Output: Compiler error



Rule 6: We can find size of a pointer using sizeof operator.

What will be output of following c program?


#include<stdio.h>

int main(){

int near*far*huge* p;

printf("%d",sizeof(p));
printf(" %d",sizeof(*p));
printf(" %d",sizeof(**p));


return 0;
}

Output: 4 4 2