C language-string

+1 vote
asked Mar 1, 2018 by anonymous
Input a word and a letter. If the letter is appearing in word,output on which position.

example:

Input Apple

Input l

output: letter l is appearing in 4 position in word Apple

3 Answers

0 votes
answered Mar 2, 2018 by anonymous
#include <stdio.h>
#include <string.h>
int main()
{
    char *word="apple";
    char letter='l';
    char *e =strchr(word, letter);
    printf("position of %c is %d in %s", letter, (int)(e-word),  word);
    return 0;
}

0 votes
answered Mar 3, 2018 by anonymous
I got 3 errors:

Error    3    error C2143: syntax error : missing ';' before 'type'   

Error    4    error C2065: 'e' : undeclared identifier  

Error    5    error C2113: '-' : pointer can only be subtracted from another pointer
0 votes
answered Mar 3, 2018 by anonymous
#include <stdio.h>

int main()
{
    char s[10],ch;
    int i;
    printf("Enter any word: ");
    gets(s);
    printf("enter any char: ");
    scanf("%c",&ch);
    for(i=0;s[i]!=0;i++)
    
    {
        if(ch == s[i])
        {
            printf("entered char and position  is: %c %d \n",ch,i+1);
        }
        
    }

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...