Search a Character in a String Using a Pointer in C

Introduction

In this post, there is a program to search a character in a string using a pointer in c programming. Pointer is a variable where we can store the address of another variable.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

For better understanding, I am going to write two programs in this post. In both programs, we will be able to search a string. One will use a pointer and one will not use the pointer variable.

How to Search a Character in a String?

We are going to search for a character in a string means we are interested in the location of that character in the string. For this, we are using the following steps.

  1. Read a string from the user.
  2. Read a character from the user to search in the given string.
  3. Repeat following steps until we get null character.
  4. Compare a character with each and every character from the string.
  5. If we get our character, then print the location of that character and exit.
  6. If we reach at the end of the string without getting the character, then print the message “not found ” and exit.

As you can see, this is a very simple program to search a character in a string. In the next sections, I am going to write both these programs. But, before that let us see the expected output.

Also Read: C Program to Print Multiples of 5 using do-while loop

Expected Output

search a character in a string using a pointer in c
Fig. search a character in a string using a pointer in c

Search a Character in a String Without Using a Pointer in C

In this program, we are not using a pointer but the output will remain the same.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char ch, str[50];
    int i=0,flag=0;
    printf("Enter a String:\n");
    gets(str);
    printf("Enter a character to search in a string:\n");
    scanf("%c",&ch);
    while(str[i]!='\0')
    {
        if(ch==str[i])
        {
            printf("%c character is found at %d location\n",ch,(i+1));
            flag=1;
            break;
        }
        i++;
    }
    if(flag==0)
    {
        printf("Character %c is not found in the string %s\n",ch,str);
    }
    return 0;
}

In the above, we have written a program to search a character without using a pointer. Now, I am going to write the same program using a pointer.

Also Read: C Program to Remove Zeros from a number

Search a Character in a String Using a Pointer in C

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char ch, str[50];
    int count=0,flag=0;
    char *ptr;
    ptr=str;
    printf("Enter a String:\n");
    gets(str);
    printf("Enter a character to search in a string:\n");
    scanf("%c",&ch);
    while(ptr!='\0')
    {
        if(ch==*ptr)
        {
            printf("%c character is found at %d location\n",ch,(count+1));
            flag=1;
            break;
        }
        ptr++;
        count++;
    }
    if(flag==0)
    {
        printf("Character %c is not found in the string %s\n",ch,str);
    }
    return 0;
}

Here, I have declared one character pointer ptr. The statement ptr=str will assign the address of str[0] in the pointer variable ptr.

Also Read: The while loop in C Programming

In the while loop, we are going to compare the character with the ptr. If we get our character then we will display the message that the character is found. But if we did not get our character then we will increment the pointer variable ptr and so on.

I hope you have understood this program. If you have any difficulty then please feel free to contact me.

Thank you.

Some Important C Programs

  1. Program in C to Find Longest Line in a File
  2. Palindrome in C using Pointers
  3. Insert and Delete element in Array in C using switch case
  4. C Program to Add Alternate Elements of a 2D Array
  5. Arrays in C for Complete Beginners
  6. C Program to Find Area of a Circle using Preprocessor
  7. Program in C to Remove White Spaces and Comments from a File
  8. C Program to Print Numbers Except Multiples of n
  9. Reverse a Number using getchar and putchar function in c
  10. The while loop in C Programming

FAQs

Can you use pointers with strings?

Yes. In c programming, the string is nothing but a character array. So you can access each and every character in a string using a pointer.

How do I print a string with pointers?

int main()
{
char str[50];
char ptr;
ptr=str;
printf(“Enter a string\n”);
gets(str); printf(“You have entered: “);
while(ptr!=’\0′)
{
printf(“%c”,*ptr);
ptr++;
}
return 0;
}
The output of this program.
How do I print a string with pointers?

Leave a Comment