You must Sign In to post a response.
  • Category: Programming

    Implementation of program to search a character in a string using pointer.


    Preparing for Computer Programming examination? ? Wondering about how to use pointers to search a character in a string? Scroll through this page and read the answers from our ISC experts for using pointers in a program.

    Following is the question asked in MSBTE(Maharashtra State Board of Technical Education, Mumbai) Diploma in Computer Engineering question paper for Summer 2017 examination.
    This question is asked for 4 marks and hence requires some explanation along with program code. Please let me know the solution.
    Q) Write a program to search a character in a string using pointer. [4 Marks]
  • Answers

    1 Answers found.
  • Pointer is variable which is able to hold address of another appropriate data type variable. In C++ when a string is assigned to character array then array name indicates address of it's zeroth location. So if we declare a character pointer variable like char * p; and a character array like char str[20]; Then the statement p=str causes pointer p to hold the address of zeroth location character of string str. This concept is used in this program.

    Below is the program to search required character in a string using pointer.
    Please add iosream.h and conio.h header files before declaration of main function.

    //PROGRAM TO FIND REQUIRED CHARACTER IN THE STRING USING POINTER.
    #include
    #include
    void main(void)
    {
    char str[20],*p,find;
    int k=0,len,c=0,d=0;
    clrscr();
    cout<<"Enter any string\n";
    cin>>str;
    cout<<"\nEnter the character to find out\n";
    cin>>find;
    p=str;
    while(*p!='\0')
    {
    c++;
    p++;
    }
    len=c;

    clrscr();
    cout<<"Given string is "< cout<<"length of given string is "< cout<<"required character "< for(p=str;p {
    d++;
    if(*p==find)
    {
    k=1;
    cout<<"\nat "< }
    }
    if(k==0)
    {
    cout<<"\nRequired character is not found in the given string ";
    }
    getch();
    }
    In above program length of given string is also calculated by using pointer to string concept instead of using ready made strlen().


  • Sign In to post your comments