Function PLzzzzzzzzzzz

A

ash4640

1.Guys how do i write a function similar to the strcmp() in the librar
like some xstrcmp().
2. How do i write another function eg x() which compares two strings
const char *string, const char *strSearch ie
*string="Hai hello"
*strSearch="ai"
I have to search for ai in the string "Hai Hello" and then return "a
Hello" if found or return NULL if not found.
Hope You can guys can help out a newbie if you cannot get me the whol
func get me some prototype atleast so I can work it out.
Also try to get me the first strcmp() func.
regards
Ash


-
ash464
 
E

Eric Sosman

ash4640 said:
1.Guys how do i write a function similar to the strcmp() in the library
like some xstrcmp().

#include <string.h>
int xstrcmp(const char *p, const char *q) {
return strcmp(p, q);
}
2. How do i write another function eg x() which compares two strings
const char *string, const char *strSearch ie
*string="Hai hello"
*strSearch="ai"
> I have to search for ai in the string "Hai Hello" and then return "ai
> Hello" if found or return NULL if not found.

#include <string.h>
char *xstrstr(const char *string, const char *strSearch) {
return strstr(string, strSearch);
}
Hope You can guys can help out a newbie if you cannot get me the whole
func get me some prototype atleast so I can work it out.
Also try to get me the first strcmp() func.
regards
Ash.

> Guys also how would i write a function for converting a decimal no
> to a binary and binary to decimal no a prototype is enough.

int dec_to_bin(int x) {
return x;
}

int bin_to_dec(int x) {
return x;
}

Three comments: First, the two string-bashing problems
sound very much like homework, and few Usenet denizens like
to do other people's homework -- we'll usually help you fix
code you've already tried to write, but seldom will we write
it for you. Second, the binary/decimal problem shows either
a fundamental misunderstanding of computer arithmetic or an
inability to state the problem correctly: are you trying to
convert between numbers and strings, or is the goal something
else entirely? Third, asking for help only from "guys" cuts
you off from a potentially valuable resource.
 
R

red floyd

Eric said:
[blatant homework request redacted]
Three comments: First, the two string-bashing problems
sound very much like homework, and few Usenet denizens like
to do other people's homework -- we'll usually help you fix
code you've already tried to write, but seldom will we write
it for you.

Well, if he had posted his professor's address so that we could mail it
directly to the prof.... :)
 
D

Default User

ash4640 said:
1.Guys how do i write a function similar to the strcmp() in the library
like some xstrcmp().



Most of use some sort of text editor to produce C code. This can be a
stand-alone editor for a command-line compiler, or part of an integrated
development environment.

Hope this helps.




Brian Rodenborn
 
M

Mike Wahler

ash4640 said:
1.Guys how do i write a function

When you've learned C, you'll know how to write a function.
similar to the strcmp() in the library

In order for your function to be 'similar', you'll
need to find out what 'strcmp()' does.
like some xstrcmp().
2. How do i write another function eg x() which compares two strings
const char *string, const char *strSearch ie
*string="Hai hello"
*strSearch="ai"

Do the same as above, but look up what the 'strstr()'
function does.
I have to search for ai in the string "Hai Hello" and then return "ai
Hello" if found or return NULL if not found.

IOW if the string is found, return a pointer to it,
otherwise return NULL.

Nit: finding a substring (which is what you describe) is not
the same thing as 'comparing two strings'. Comparing two
strings is what 'strcmp()' (and hopefully eventually your
version of it) does.
Hope You can guys can help out a newbie

We'll certainly help, but we won't do it for you. Post
your code and ask specific questions.
if you cannot get me the whole
func get me some prototype atleast so I can work it out.

Prototypes (based upon those of the corresponding standard
library functions):

int xstrcmp(const char *s1, const char *s2);
const char *xstrstr(const char *s, const char *to_find);
Also try to get me the first strcmp() func.

No, we won't do it for you. Post your best attempt
and ask specific questions.

-Mike
 
M

Malcolm

ash4640 said:
1.Guys how do i write a function similar to the strcmp() in the library
like some xstrcmp().
Do you understand what a prototype is? It tells the compiler, and
secondarily the human programmer, what the function returns and what
parameters it takes. So for instance a function to calculate a sine would
take a double (in degrees or radians) and return a double.

prototype:
double sine(double radians);

Your prototype for xstrcmp() should be exactly the same as the prototype for
strcmp().
2. How do i write another function eg x() which compares two strings
const char *string, const char *strSearch ie
*string="Hai hello"
*strSearch="ai"
I have to search for ai in the string "Hai Hello" and then return "ai
Hello" if found or return NULL if not found.
Hope You can guys can help out a newbie if you cannot get me the whole
func get me some prototype atleast so I can work it out.
Also try to get me the first strcmp() func.
Getting the prototype written is half the battle. This should always be the
first thing you write, when writing a function.

For general string manipulation, chars are just small variables which
conventionally contain character information. Howver then can be compared
like other functions.
You also need to understand how to increment pointers. In C, strings are
null-terminated, so you need to increment the pointer you are passed,
examining each character, until you come to the NUL, which indicates the end
of the string.

Then both of these functions should be easy to write.
 
P

Paul

Eric Sosman said:
#include <string.h>
int xstrcmp(const char *p, const char *q) {
return strcmp(p, q);
}


#include <string.h>
char *xstrstr(const char *string, const char *strSearch) {
return strstr(string, strSearch);
}




int dec_to_bin(int x) {
return x;
}

int bin_to_dec(int x) {
return x;
}

Three comments: First, the two string-bashing problems
sound very much like homework, and few Usenet denizens like
to do other people's homework -- we'll usually help you fix
code you've already tried to write, but seldom will we write
it for you. Second, the binary/decimal problem shows either
a fundamental misunderstanding of computer arithmetic or an
inability to state the problem correctly: are you trying to
convert between numbers and strings, or is the goal something
else entirely? Third, asking for help only from "guys" cuts
you off from a potentially valuable resource.

Third, asking for help only from "guys" cuts you off from a
potentially valuable resource.

one of those funniest statements, had a hearty laugh, thanks pal.

-Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top