Welcome Guest ( Log In | Register )




                Web Hosting Guide

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
 
New Topic
Replies (1 - 2)
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: 160
Joined: 7-November 05
Member No.: 9,489
myCENTs:36.33


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   11 FirefoxRocks 1,987 15th March 2010 - 08:29 AM
Last post by: iG-StanB
No New Posts   15 Normano 1,462 23rd February 2010 - 10:34 PM
Last post by: iG-
No New Posts 4 kanade 1,191 23rd February 2010 - 09:51 PM
Last post by: iG-ryanul chowdhury
No New Posts 7 Jonnyabc 189 17th February 2010 - 08:31 AM
Last post by: mastercomputers
No new   24 lacking_imagination 6,532 15th February 2010 - 03:48 AM
Last post by: iG-richard
No New Posts   16 sandeep 2,670 8th February 2010 - 09:19 AM
Last post by: iG-Vikram
No new 49 bigd1 8,475 6th February 2010 - 09:07 PM
Last post by: iG-t7ancients
No new   103 miCRoSCoPiC^eaRthLinG 14,487 1st February 2010 - 12:30 PM
Last post by: 8ennett
No New Posts   11 xboxrulz 1,553 21st January 2010 - 06:44 PM
Last post by: iG-Komputer professor
No New Posts   4 Manu Dhanda 1,199 6th January 2010 - 12:39 PM
Last post by: iG-medhatalbashaa
No New Posts   2 turbopowerdmaxsteel 7,997 4th January 2010 - 07:29 AM
Last post by: iG-v_tarasu
No New Posts   13 WeaponX 1,893 31st December 2009 - 03:00 AM
Last post by: iG-flip
No New Posts   1 miCRoSCoPiC^eaRthLinG 1,361 31st December 2009 - 01:16 AM
Last post by: iG-mahesh Chander
No New Posts   2 Feelay 459 9th December 2009 - 01:26 PM
Last post by: wutske
No New Posts   17 MOOSE 2,944 3rd December 2009 - 02:01 PM
Last post by: Spencer


Web Hosting Powered by ComputingHost.com.
HONESTY ROCKS! truth rules.
Creative Commons License