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.
| |
|
Welcome to AstaHost - Dear Guest | |
Toggle shoutbox
Shoutbox
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Can Anyone Solve This Question In C Programming Language?
Started by Stella Richards, May 04 2009 07:52 AM
2 replies to this topic
#2
Posted 05 May 2009 - 06:24 AM
Hi Stella,
Just a quick question about your problem. Is "x" a string or a character?
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.
#3
Posted 29 May 2009 - 08:02 PM
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, 29 May 2009 - 08:04 PM.
Reply to this topic
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users











