Welcome Guest ( Log In | Register )




                Web Hosting Guide

 
Reply to this topicNew Topic
Can Anyone Solve This Question In C Programming Language?
Stella Richards
post May 4 2009, 07:52 AM
Post #1


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 1
Joined: 4-May 09
Member No.: 40,052


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.

Go to the top of the page
 
+Quote Post
PureHeart
post May 5 2009, 06:24 AM
Post #2


Premium Member
Group Icon

Group: Members
Posts: 212
Joined: 7-October 05
From: RMIT University
Member No.: 8,966
myCENTs:65.61


Hi Stella,

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

QUOTE (Stella Richards @ May 4 2009, 02:52 PM) *
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.

Go to the top of the page
 
+Quote Post
magiccode9
post May 29 2009, 08:02 PM
Post #3


Advanced Member
Group Icon

Group: [HOSTED]
Posts: 145
Joined: 7-November 05
Member No.: 9,489
myCENTs:33.08


try this code to see if it is help ~~

CODE
#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;
}


This post has been edited by magiccode9: May 29 2009, 08:04 PM
Go to the top of the page
 
+Quote Post

Reply to this topicNew Topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   8 xboxrulz 1,243 Yesterday, 08:31 PM
Last post by: iG-Anon
No New Posts   15 david_ytk 1,855 6th November 2009 - 12:31 PM
Last post by: The_Fury
No New Posts   6 eyvind 1,974 6th November 2009 - 01:52 AM
Last post by: The_Fury
No New Posts   5 sparkx 684 4th November 2009 - 12:29 AM
Last post by: HannahI
No New Posts   14 sandeep 2,276 3rd November 2009 - 09:11 PM
Last post by: iG-rick
No new   19 lonebyrd 3,521 1st November 2009 - 03:49 PM
Last post by: iG-Lukenda
No New Posts   0 MikyZuma 48 24th October 2009 - 10:28 AM
Last post by: MikyZuma
No New Posts   12 WeaponX 1,609 8th October 2009 - 07:59 PM
Last post by: HannahI
No New Posts   1 acdragon 803 5th October 2009 - 08:47 PM
Last post by: HannahI
No New Posts   14 Normano 1,196 3rd October 2009 - 07:44 PM
Last post by: iG-anonymous
No New Posts 2 kanade 831 9th September 2009 - 09:25 AM
Last post by: iG-Mohan
No New Posts   1 t3jem 3,186 20th August 2009 - 01:01 PM
Last post by: iG-Patrick Coffman
No new 38 bigd1 7,159 15th August 2009 - 11:08 AM
Last post by: The_Fury
No New Posts   18 knight17 6,191 10th August 2009 - 05:29 AM
Last post by: iG-Helping
No New Posts   1 khan4lyfe 57 29th July 2009 - 09:46 PM
Last post by: tansqrx


Web Hosting Powered by ComputingHost.com.