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

ANSI C ------ question .

P: 27
Please help me . I am new at C , Learing C will take time but i have to submit this and i dont know how to do it honestly so please help anyone .
thanks in advance .
______________________________________________

Write 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 3 '06 #1
Share this Question
Share on Google+
3 Replies


P: 6
Hi,

This function should work...

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. //function declaration
  6. char *mystrcat(char *s, char *p);
  7.  
  8. int main()
  9. {
  10. printf("%s",mystrcat("David","Jones"));
  11. }
  12.  
  13. //function defination
  14. char *mystrcat(char *s, char *p)
  15. {
  16. static int k;
  17.  
  18. //Get length of string s and string p without using strlen function
  19. int i = 0;
  20. while( *(s+i) != '\0')
  21. i++;
  22. int j = 0;
  23. while( *(p+j) != '\0')
  24. j++;
  25.  
  26. //define a char pointer to hold appended string
  27. char * ch;
  28. ch = (char *)calloc(i+j+1,sizeof(char));
  29.  
  30.  
  31. //Appended strings
  32. for(;k<i; k++)
  33. *(ch+k) = *(s+k);
  34. for(int l=0;l<j;l++,k++)
  35. *(ch+k) = *(p+l);
  36.  
  37. return(ch);
  38. }
  39.  
  40.  
Nov 3 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
This function should work...
a. Don't do peoples course work for them because they will learning nothing, feel free to give advice and hints


b. The solution you have posted doesn't answer the question and doesn't work either

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. //function declaration
  6. char *mystrcat(char *s, char *p);
  7.  
  8. int main()
  9. {
  10. printf("%s",mystrcat("David","Jones"));
  11. }
  12.  
  13. //function defination
  14. char *mystrcat(char *s, char *p)
  15. {
  16. /* What is this static keyword doing here, it is completely 
  17.    unrequired and has the effect of making the function non-reentrant */
  18. static int k;
  19.  
  20. //Get length of string s and string p without using strlen function
  21. int i = 0;
  22. while( *(s+i) != '\0')
  23. i++;
  24. int j = 0;
  25. while( *(p+j) != '\0')
  26. j++;
  27.  
  28. /* This code does not append p to the end of s it cretaes a 
  29.    new string with s and p in it.  Worst still it hides a memory 
  30.    allocation making it a candidate to cause memory leaks */
  31.  
  32. //define a char pointer to hold appended string
  33. char * ch;
  34. ch = (char *)calloc(i+j+1,sizeof(char));
  35.  
  36. /* You have used k without initialising it, since it is static it will 
  37.    probably have a value of 0 first time round but second time round
  38.    it will have the same value if finished on last time.
  39.    This will result in a write behond the end of allocated memory
  40.    which is invokes undefined behaviour and most likely will 
  41.    cause a program crash. */
  42.  
  43. //Appended strings
  44. for(;k<i; k++)
  45. *(ch+k) = *(s+k);
  46. for(int l=0;l<j;l++,k++)
  47. *(ch+k) = *(p+l);
  48.  
  49. /* The question specifically calls for the function to return the pointer s
  50.    This does not do it.*/
  51. return(ch);
  52. }
  53.  
This whole function can be written using 1 variable of type char *
Nov 3 '06 #3

P: 27
Hey The Thing Is I Am New At C Programming So I Dont Know That Much , But U Know How The Teachers Are !
I Need To Do This . I Am On My Way Of Learning Ansi C , So If Anyone Could Post The Right Answer Then I Will Appreciate It . I Will Go Through The Right Code And Then Understand Each Line . So In A Way U Will Be Helping Me Learn Ansi C .
Thanks For David But That Code Is Wrong .
Can Anyone Do The Right One Please.
Thanks
Nov 3 '06 #4

Post your reply

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