How to search a letter in words with POINTER

Hi expert..Im newbie and i want to made a program to search a letter using a pointer..
Example there is a word WORLD
then the program will ask what letter u want to looking for..example its R and the program will said R is in index 3 and all R letter in the word is 1..CAN ANYBODY HELP ME
Last edited on
Based on the need to use pointers, you're accessing letters stored in an array of characters, so, the program would go somewhat like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    char arr[] = "WORLD";
    cout << "Enter a letter to search for\n";

    char c;
    cin >> c;

    char* beg = arr;
    char* end = arr + sizeof arr;
    char* pos = find(beg, end, c);
    int cnt = count(beg, end, c);

    if(pos == end)
        cout << "Not found\n";
    else
        cout << "The letter " << c << " found " << cnt << " times. "
             << "The first time at position " << 1 + pos - beg << '\n';
}

online demo: http://ideone.com/SmCg3

Note that this is case-sensitive, enter r instead of R and it will say 'not found'
(edit: missed the count requirement, added)
Last edited on
Hmm..i don't know that for sure im newbie could you tell me if the word dinamic..??
@Cubbi
,

you have demonstrated some halved approach. If you used algorithm then it is more natural to use also standard functions sdt::begin and std::end. So either you should write function find yourself or you use all standard functions to make the approach more complete.:)

@Neo Takaredase


In fact you need two functions. One will search a given character in a character array and other will count how many times the character is present in the array.

I will show the first function

1
2
3
4
5
6
7
size_t find( const char s[], char c )
{
   const char *p = s;
   while ( *p && *p != c ) ++p;

   return ( p - s );
}


In the main you will use the following test to determine whether the character is found.

1
2
3
size_t pos = find( word, 'R' );

if ( pos != std::strlen( word ) ) cout << "The letter " << 'R' << " is found at position " << pos << '\n';
Last edited on
i dont even know how to use alogarithm..any help how to use it?
@vlad
what is the 2nd function i can't make it @@
It is almost the same as the first.

1
2
3
4
5
6
7
8
9
10
size_t count( const char s[], char c )
{
   size_t n = 0;
   for ( const char *p = s; *p; ++p )
   {
      if ( *p == c ) ++n;
   }

   return ( n );
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <algorithm>
#include <conio.h>
using namespace std;
size_t find( const char s[], char c )
{
   const char *p = s;
   while ( *p && *p != c ) ++p;
   return ( p - s );
}
size_t count( const char s[], char c )
{
   size_t n = 0;
   for ( const char *p = s; *p; ++p )
   {
      if ( *p == c ) ++n;
   }

   return ( n );
}

int main()
{   char word[100];
    cout << "Enter a word : ";
    cin>>word;
    cout << "Enter a letter to search for : ";
    char c;
    cin >> c;
    size_t pos = find ( word , c );
    size_t count = count ( word ,c );
    if ( pos != std::strlen( word ) ) cout << "The letter " << c << " is found at position " << pos << "The number of letter is" << count <<'\n';
    
    getch();
}


is it right?? but it's look like it wrong haha...
You need no header <algorithm> because you do not use any function from it.
As for the program if you get a correct result then the program is correct. By the way the position of a letter starting from 0.

Also you have to check that the position is not equal to std::strlen( word ) that means (in case of the equality) that the character is not found. So to use function std::strlen you have to include header <cstring>
Last edited on
ok thanks vlad...that really help me thanks a lot..
Topic archived. No new replies allowed.