Jump to content
xisto Community
Stella Richards

Can Anyone Solve This Question In C Programming Language?

Recommended Posts

Hi,

 

Write code to solve the following problem. Define a function that takes a String as a parameter and returns an integer value. The function need to loop through each character in the input string to find a value of “x”. If a value of “x” is found in the string then return the position within the string where “x” is found else return -1.

Share this post


Link to post
Share on other sites

Hi Stella,

 

Just a quick question about your problem. Is "x" a string or a character?

 

Hi,

 

Write code to solve the following problem. Define a function that takes a String as a parameter and returns an integer value. The function need to loop through each character in the input string to find a value of “x”. If a value of “x” is found in the string then return the position within the string where “x” is found else return -1.

Share this post


Link to post
Share on other sites

try this code to see if it is help ~~

#include <stdio.h>int findpos(char *str, char x);int main(int argc, char **args){[tab][/tab]char *s = "some gext";[tab][/tab]int r;[tab][/tab]r = findpos(s, 'g');[tab][/tab]printf("%d\n", r);}int findpos(char *str, char x){[tab][/tab]char *s = str;[tab][/tab]int i = 0;[tab][/tab]while (*s && (*s != x))[tab][/tab]{[tab][/tab][tab][/tab]++i; ++s;[tab][/tab]}[tab][/tab]return (*s) ? i : -1;}

Edited by magiccode9 (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.