Find First Occurrence of a Character in String in C Language

Find-First-Occurrence-of-a-Character-in-String-in-C-Language

Program Description:

Write a Program to Find First Occurrence of a Character in String in C Programming language. The program should accept a string and a character from the user and find the first occurrence of the given character in the string.

The program is case-sensitive. So the Uppercase character and Lowercase characters are different.

📢 This Program is part of the C String Practice Programs series

We will look at a few methods to search for the first occurrence of a character in the string program. Each method will contain a step-by-step explanation of the program with program output. We also included the instructions to compile and run the program using your compiler.

Before going to write the program. Let’s understand the program requirements by looking at an example input and output of the program.

Example Input and Output:

Input:

Enter a string : CProgramming

Enter character to search in the string: o

Output:

FOUND: First Occurance of o character is found at 3 index in given string

The character o is at the index 3 in the input string CProgramming

Prerequisites:

To gain a better understanding of the program. Please review the C language string tutorials below.

We will look at the three different methods to search for the character in a string in C Language.

  • Iterative Method
  • User-defined Functions
  • Library Function ( strchr library function)

First Occurrence of a Character in String in C Program Explanation:

  1. Start the program by defining a Macro to hold the max size of the array – #define SIZE 100
  2. Declare the input string( inputStr)
  3. Take the input string and character to search from the user and update the inputStr and ch variables. We use the gets function to read the string.
  4. To search for the first occurrence of the character( ch) in a string( inputStr), Iterate over the string and check each character.
  5. For loop starts by initializing i to 0. It will continue looping as long as the current character ( inputStr[i]) is not a NULL character ( '\0'). At each iteration, i is incremented by 1.
    • Check if the current character( inputStr[i]) is equal to ch- i.e if(inputStr[i] == ch)
    • If the above condition is true, Then we found the character( ch) in the inputStr. print the result and stop the program.
  6. If we reached out of the loop, Then ch is not found in the inputStr. So print the NOT FOUND message on the console.

Method 1: First Occurrence of a Character in String in C Program (Iterative Method):

Here is the program to search for a character in a string in C programming language.

Program Output:

Let’s compile and run the program.

Compile the program.

$ gcc search-character.c

Run the program.

find-first-occurrence-of-character-in-string-program-output

In the above example, the user provided the input string as CProgramming and the character to search is o.

The program is able to find the character o at the index 3 in the given CProgramming string.

Let’s look at a few more examples.

The program is generating the excepted results.

Method 2: First Occurrence of a Character in String using user-defined functions:

In the above program, We have written the complete program inside the main function. It is not a recommended method, Let’s rewrite the above program to use a function search for the character in the string.

Define a function named searchChar() to search the character in a string.

Here is the prototype of the searchChar() function.

int searchChar(char * str, int ch)

Search for the character 'ch'in String(‘ str’) and If the ch is found in the str return the index of ch. Otherwise, return -1.

Here is the modified program to search for a string using a user-defined function

Call the searchChar() function from main() function and pass the input string and character( ch).

Capture the return value in result variable.

Program Output:

Compile and Run the program.

first-occurrence-of-character-in-string-using-functions

In the first example, The character m is found at the 6th index in Programming is Fun string. Similarly, In the second string, The character r is not found in the string Tech

Method 3: First Occurrence of a Character in String using strchr function in C:

In this method, Let’s use the strchr library function to find first occurrence of a character in string in c programming language.

Program Output:

Let’s compile and run the program.

find-first-occurrence-of-character-in-string-using-strchr-function

As we can see from the above examples, The program is generating the desired results.

Related String Practice Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply