Find The Location Of a Character in a String

-- Leave a Comment
Hello Here is C Program to Find the Location (Place) of a Character That one Lies at Last.
Example: In Apple find P. Answer is 2. In Sagar Find a. Answer is 3. Else Not Found print no found.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
#include<conio.h>
void main()
 {
   char a,name[10];
   int i,the=0;
   printf("Enter String:- ");
   gets(name);
   printf("Enter Char:- ");
   scanf("%c",&a);
   for(i=0;name[i]!='\0';i++)
   the=the+1;
   for(i=the;i>=0;i--)
   {
    if(a==name[i])
    {
      printf("%d",i);
      break;
      }
   }
   if(i<0)
   printf("Not Found Sorry");
   getche();
   }

And First we find the total character and run the loop from Back and find the the character if found break the loop. else Display not found.

Hello This is Sagar Devkota. Studying Bachelor of Software Engineering at Pokhara University. I know something about Linux, Android, Java, Nodejs, Unity3D and 3 dots :)

0 comments:

Post a Comment