Write a program to find a character from the string, string and character to
be searched both will be given by user.
Answers
Answer:
String is an array of characters. So, suppose if I take an array, then how the logic becomes? I have an array(string) of elements(characters). I will now find a character i.e, compare each character of my string to the search character. So if match found the peogram returns true else false.
So this is how the program will look like
//in C//
//including necessary libraries//
int main(void)
{
char x[30]; //string(array of char.)
char c; //searching char.
int i;//for initialising loop
clrscr();
printf("enter the string:");
scanf("%s",x);
printf("enter the char to which its position is to be found:");
scanf(" %c",&c);
for(i=0;x[i]!=NULL;i++)
{
if(x[i]==c)
{
printf("the position is:%d",i);
}
}
getch();
}
Note that some compilers may give error while compairing characters. In that case you may compare the character values by their ASCII value.
Hope this helps. Comment if any other help needed.
Answer:Write a program to find a character from the string, string and character to
be searched both will be given by user.
Explanation: