Stella Richards Posted May 2, 2009 Share Posted May 2, 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. Quote Link to comment https://forums.phpfreaks.com/topic/156523-can-anyone-solve-this-question-in-c-programming-language/ Share on other sites More sharing options...
Mchl Posted May 2, 2009 Share Posted May 2, 2009 Is this request compliant with forum rule #19? http://www.phpfreaks.com/page/forum-rules Quote Link to comment https://forums.phpfreaks.com/topic/156523-can-anyone-solve-this-question-in-c-programming-language/#findComment-824185 Share on other sites More sharing options...
corbin Posted May 2, 2009 Share Posted May 2, 2009 Also, it's a pretty simple question which implies that you haven't even tried at all. Can you use C++? If so, you could just wrap it in an std::string object and use string's find() method. If not, I'm assuming by string you mean a null terminated array of characters. (Not important if it's not null terminated.) In that case, just loop through the array looking for "x". Homework's a bitch when you don't pay attention ;p. Quote Link to comment https://forums.phpfreaks.com/topic/156523-can-anyone-solve-this-question-in-c-programming-language/#findComment-824252 Share on other sites More sharing options...
shlumph Posted May 2, 2009 Share Posted May 2, 2009 What do you have so far, Stella? Quote Link to comment https://forums.phpfreaks.com/topic/156523-can-anyone-solve-this-question-in-c-programming-language/#findComment-824258 Share on other sites More sharing options...
GingerRobot Posted May 2, 2009 Share Posted May 2, 2009 Why would you write this function anyway? Use strchr Quote Link to comment https://forums.phpfreaks.com/topic/156523-can-anyone-solve-this-question-in-c-programming-language/#findComment-824351 Share on other sites More sharing options...
Mchl Posted May 2, 2009 Share Posted May 2, 2009 Besiedes, the task is incomplete. What should be the output, if more tha one occurence of 'x' is found within a string? Quote Link to comment https://forums.phpfreaks.com/topic/156523-can-anyone-solve-this-question-in-c-programming-language/#findComment-824353 Share on other sites More sharing options...
Daniel0 Posted May 3, 2009 Share Posted May 3, 2009 Why would you write this function anyway? Use strchr To learn how to implement simple algorithms in C, I would assume. This is quite clearly a homework assignment. Quote Link to comment https://forums.phpfreaks.com/topic/156523-can-anyone-solve-this-question-in-c-programming-language/#findComment-825150 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.