C Program to Find First and Last Occurrence of Character in a String

This is a C Program to find first and last occurrence of given character in a string.

Problem Description

This program takes a string and a character as input and finds the first and last occurrence of the input character in a string.

Problem Solution

1. Take a string and a character as input.
2. Using for loop search for the input character.
3. When the character is found, then print its corresponding position.
4. Again keep on searching for the input character. Now keep on incrementing a variable whenever the input character encounters.
5. Do step-4 until the end of string. when it is done, print the value of the variable.

Program/Source Code

Here is source code of the C Program to find first and last occurrence of given character in a string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C Program to find First and Last Occurrence of given 
  3.  * Character in a String
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. void main()
  9. {
  10.     int i, count = 0, pos1, pos2;
  11.     char str[50], key, a[10];
  12.  
  13.     printf("enter the string\n");
  14.     scanf(" %[^\n]s", str);
  15.     printf("enter character to be searched\n");
  16.     scanf(" %c", &key);
  17.     for (i = 0;i <= strlen(str);i++)
  18.     {
  19.         if (key == str[i])
  20.         {
  21.             count++;
  22.             if (count  == 1)
  23.             {
  24.                 pos1 = i;
  25.                 pos2 = i;
  26.                 printf("%d\n", pos1 + 1);
  27.             }
  28.             else 
  29.             {
  30.                 pos2 = i;
  31.             }
  32.         }
  33.     }
  34.     printf("%d\n", pos2 + 1);
  35. }
Program Explanation

1. Take a string and a character as input and store it in the array str[] and variable key respectively.
2. Using for loop search for the variable key. If it is found then increment the variable count.
3. If the value of count is equal to 1, then copy the value of i into the variables pos1 and pos2 and print the value (pos+1) as the first position.
4. If the value of count is not equal to 1, then just copy the value of i into the variable pos2. Do this step until the end of string.
5. Print the value (pos2+1) as the last position and exit.

advertisement
advertisement
Runtime Test Cases
enter the string
welcome to sanfoundry's c programming class!
enter character to be searched
m
6 
34

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.