By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

One simple help........

P: 27
hi all i am a new bee for ANSI C , can anyone solve the question for me please , so that i can understand each line of it wid puting time into it , i really want to get ANSI C in my head so kindly help me out .
cheers.





A function with the following prototype

Char *mystrcat(char *s, char *p);

The function appends string p to the end of string s and returns a pointer to the start of string s.
Remember that strings are terminated by binary zero (‘\0’).

Suppose string s contains “abcd” and string p contains “xyz”. The result would be string s containing “abcdxyz”.

Use the program section below to test your program:
#include <ctype.h>

char *mystrcat(char *s,char *p);

main()
{
Char string1[80], string2[80];
For ( ; ; )
{
Printf(“Enter string1: “);
Scanf(“%s”, &string1);
If (!strcmp(string1, “done”))
Break;
Printf(“Enter string2: “);
Scanf(“%s”, &string2);
Printf(“String1: %s String2: %s\n”, string1, string2);
Printf(“Returned string : %s\n”, mystrcat(string1,
string2);
Printf(“String1: %s String2: %s\n”, string1, string2);
}
}

Hint : ctestchar = *(ptrstring++) – assigns the character pointer to by ptrstring to the character ctestchar and moves the pointer on by one character.
Nov 7 '06 #1
Share this Question
Share on Google+
16 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Marsguy-

What have you tried on this? What parts are you having touble with?

(you're more likely to get help if you ask a question, and it doesn't look like we're just doing your homework for you ;) )
Nov 7 '06 #2

P: 27
Well as i said that i am a new bee and will understand it only after i go throu the code for it . I was honest in saying it and not making up excuses . If u dont want to help then its ok friend.
Nov 7 '06 #3

sicarie
Expert Mod 2.5K+
P: 4,677
Well as i said that i am a new bee and will understand it only after i go throu the code for it . I was honest in saying it and not making up excuses . If u dont want to help then its ok friend.
I'm not saying I don't want to help, but you have asked no questions, it looks like you have just posted your homework problem up, and - I'm sorry - I am not going to just give you the code, though if you try it, actually have your own question (like "where do i start" is a valid question), then I'd be more than happy to respond. If you're willing to learn it, i will do everything I can to help you do that, but it looks as if you haven't even bothered to try the question, you just want to get the answer with as little thought and effort as possible.
Nov 7 '06 #4

P: 27
allrite where do i start ?
Nov 7 '06 #5

sicarie
Expert Mod 2.5K+
P: 4,677
allrite where do i start ?
You have to define your problem. You have yet to ask a question, and most of your code looks right. Right now you have an empty for loop at the beginning. I'm not sure why this is in a for loop, it looks more like it should be in a while loop ("while" the strings are different). All that having been said, there is no question being asked. The code looks pretty good, though I haven't run it, as it is in an empty for loop, and that alone would break it.

If it's just compiling, the only syntax error I see is the for loop, you should figure out what you want there and get it working, then see what's going on.

What are you having trouble with? Is it not compiling? Can you not figure out what to put in the for loop? Are you looking to understand what each of the lines means?
Nov 7 '06 #6

P: 27
so far i have been able to write ----------


#include<stdio.h>
#include<conio.h>
#include <ctype.h>
#include<string.h>

char *mystrcat(char *s,char *p);
int main()
{
char string1[80], string2[80];
for( ; ; )
{
printf("Enter string1: ");
scanf("%s", &string1);


if (!strcmp(string1,"done"))
break;

printf("Enter string2:");
scanf("%s", &string2);
printf("String1: %s String2: %s", string1, string2);
printf("\n");
printf("Returned string : %s\n", mystrcat(string1,string2));
printf("String1: %s String2: %s\n", string1, string2);
getch();
}
return 0;
}



my question is --- wats next ?
Nov 7 '06 #7

sicarie
Expert Mod 2.5K+
P: 4,677
Ok, in what sense. What part are you having trouble with?

I'm sorry, but I don't think you wrote that code yourself, and if you can't even try, I won't either.
Nov 8 '06 #8

P: 27
Just u answer to there --- i did that , and i dont think u can help coz u know nothing and jst here to fool around wid new bee like me , u are a nerd.
Nov 8 '06 #9

sicarie
Expert Mod 2.5K+
P: 4,677
Whatever you say man, I know nothing even though I pointed out a glaring syntax problem in your code that you have yet to fix - as well as the fact that

YOU HAVE YET TO ASK A QUESTION ABOUT THE CODE

you just say 'finish this' or 'what is next'. I'm not your slave, and I'm not going to do your work for you. (and I am proudly a nerd - that's why I make the big bucks now)



PS - "what is next" would be to fix that for loop.

Like I said three or four posts ago....
Nov 8 '06 #10

sicarie
Expert Mod 2.5K+
P: 4,677
Actually what is next is to get you working on the right part.

You don't want a main statement. There is no reason for it. (that's why you don't copy code) You want to create the method mystrcat(). You should start out with the function prototype that was given to you, and get it to return a string.

Eventually, you will declare that in your teacher's code (either above or below the full main statement, but not inside), and fill in values in the for loop for testing. If you want to do it right now, just to be able to compile it, I'd start low - int i =0;i<2;i++. That way you can get two rounds of input, though you're probably going to fail after 1 the first couple of times (just until you get the bugs out).

After you do the first paragraph, you should parse through a string and find the '\0' symbol at the end, and do that to one of the strings that is passed. Then you can either go through letter by letter and add the second, or point to it.

(Seeing as how you didn't include the teacher's full requirements, a good question would have been, 'how do i start on the mystrcat method')
Nov 8 '06 #11

P: 27
you missing the whole point .... u r not my slave , all i asked for was help and not your arrogant talks . no one forced u too help me .
Nov 8 '06 #12

sicarie
Expert Mod 2.5K+
P: 4,677
you missing the whole point .... u r not my slave , all i asked for was help and not your arrogant talks . no one forced u too help me .
And I'm trying to (see above), but you're not putting forth ANY effort. If you could have simply said "help me with mystrcat" this discussion would have been much shorter!
Nov 8 '06 #13

P: 3
char a;
int i;
scanf ( "%c, %i", &a, &i ); // Correct

char string [ ];
scanf ( "%s" , &string ); // INCORRECT

string MUST NOT be preceeded with an ampersand '&'
An array-name IS AN ADDRESS.

scanf ( "%s", string ); ?? Correct
Nov 14 '06 #14

P: 3
char a;
int i;
scanf ( "%c, %i", &a, &i ); // Correct

char string [ ];
scanf ( "%s" , &string ); // INCORRECT

string MUST NOT be preceeded with an ampersand '&'
An array-name IS AN ADDRESS.

scanf ( "%s", string ); //Correct
Nov 14 '06 #15

P: 3
I thought I added a reply this morning.
It appears to have vanished.

everyone seems to be repeating a gross error in your original code:

scanf ( "%s", &string );

if string is an Array: char string[100]; or suchlike,
as an array, it is handled as an Address. It does not need the &.

try scanf ( "%s", string );

JimJ
Nov 14 '06 #16

P: 27
thanks for your help guys , i managed to get this thing working . thanks a lot .
Nov 17 '06 #17

Post your reply

Sign in to post your reply or Sign up for a free account.