Monday, January 31, 2011

Pointers in C++

Searching an element in a string through pointers.
If this program make any errors please let me know,i will try to help you out.

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
int found(char *);
main()
{const int arraysize=50;
char a[arraysize],letter;
int l,count=0;
cout<<"Enter a string:";
gets(a);
char *p;
p=a;
l=found(p);
if (l==0)
cout<<"letter not found";
else
cout<<"letter is found "<<l<<" times";
      getch();
      return 0;
      }
int found(char *a)
{
    char letter;
    int count=0;
    cout<<"Enter the letter to be find:";
    cin>>letter;
    while(*a!='\0')
    {
    if(*a==letter)
    {
                  count++;
    }
    a++;
    }
    return count;
}

No comments:

Post a Comment