Stella Richards 0 Report post Posted May 4, 2009 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
PureHeart 0 Report post Posted May 5, 2009 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
magiccode91405241511 0 Report post Posted May 29, 2009 (edited) 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 May 29, 2009 by magiccode9 (see edit history) Share this post Link to post Share on other sites