Thread: Concentation of a pointer without string functions

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    196

    Concentation of a pointer without string functions

    Hello guys, I'm wondering if my code is correct for my assignment. I'm not to sure on this whole pointer thing at the moment.

    This is my assignment
    Concentation of a pointer without string functions-screen-shot-2013-11-12-10-23-44-pm-png
    Code:
    #include <stdio.h>
    
    char *my_strcat(char *string1, char *string2)
    {
        int i, j;
        for(i=0; string1[i]!='\0'; ++i);
        for(j=0; string2[j]!='\0'; ++j, ++i)
        {
            string1[i]=string2[j];
        }
        string1[i]='\0';
        return 0;
    }
    
    
    char *my_strchr(char *string1, char c)
    {
        int i, countNumberofTimes;
        countNumberofTimes=0;
        for(i=0 ; string1[i]!='\0' ; i++)
            if(string1[i]==c)
                countNumberofTimes++;
        printf("The number of occurences is: %i\n",countNumberofTimes);
        return 0;
    }
    
    
    int main()
    {
        
        char string1[200];
        char string2[200];
        char c;
        printf("Enter the first word: ");
        scanf("%s",string1);
        printf("Enter the second word: ");
        scanf("%s",string2);
        my_strcat(string1, string2);
        printf("After the concetation function this is value: %s\n",string1);
        printf("Enter a Character that you'd like to be counted: ");
        scanf(" %c",&c);
        my_strchr(string1, c);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Tried editing title since I meant concatenation

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try reading the spec for my_strchr

    It talks about returning a pointer to the first matching char.

    You just count all the chars, print the result and return 0.


    And no, your my_strcat doesn't return what it's supposed to either.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by Salem View Post
    Try reading the spec for my_strchr

    It talks about returning a pointer to the first matching char.

    You just count all the chars, print the result and return 0.


    And no, your my_strcat doesn't return what it's supposed to either.
    Thank you! I got 50 minutes to try to fix it up. haha

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    my_strcat looks good.
    EDIT: Ahh, I didn't think to look at the return for my_strcat. Glad Salem caught it. And I'm too late anyhow.
    my_strchr on the other hand....

    It does a great job of counting the occurrences of a char and printing that out, but that is not what it's supposed to do. Re-read the assignment, it's supposed to find the first occurrence.

    Also, it would generally be considered bad design for a function to do two things (i.e. count and print). A function should do one thing and do it well. For example, a proper count function would count the occurrences of the char, and return that count to the caller of the function. Let the caller decide if and how to display it to the user. The same goes for your my_strchr function, just do what the spec says, no more and no less.

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Quote Originally Posted by anduril462 View Post
    my_strcat looks good.
    EDIT: Ahh, I didn't think to look at the return for my_strcat. Glad Salem caught it. And I'm too late anyhow.
    my_strchr on the other hand....

    It does a great job of counting the occurrences of a char and printing that out, but that is not what it's supposed to do. Re-read the assignment, it's supposed to find the first occurrence.

    Also, it would generally be considered bad design for a function to do two things (i.e. count and print). A function should do one thing and do it well. For example, a proper count function would count the occurrences of the char, and return that count to the caller of the function. Let the caller decide if and how to display it to the user. The same goes for your my_strchr function, just do what the spec says, no more and no less.
    I don't seem to understand on the part of returning a pointer for my_strcat. If you guys could dumb it down for me it would help me so much. Sometimes, I hate not being completely fluent with my english!

  7. #7
    Registered User
    Join Date
    Aug 2013
    Posts
    196
    Actually guys, I'm just going to turn it in there's no point in trying to cram something that isn't going to work in 20 minutes. I did the extra credit which is worth more than this part of the assignment. So I hope that was at least correct.

    But thank you guys so much, you guys have been such a great help to me this semester.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Hope it's not too late, maybe you can re-submit if the deadline hasn't passed yet.

    A pointer is just a value. It happens to be a value containing the address of something. Returning a pointer to is the same as returning the address of.

    You know how to get the address of something, you do so right here:
    Code:
    printf("Enter a Character that you'd like to be counted: ");
    scanf(" %c",&c);
    You get a pointer to c by doing &c.

    So lets say you found the character at string[42]. You would get a pointer to that char by...

  9. #9
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Code:
    char *strcat(char *s, const char *append)
    {
            return strcpy(s + strlen(s), append), s;
    }
    Figure out how to implement strcpy and strlen, and it should be simple enough to understand.
    i dont believe in competition in da field of cboard posts, u see, i believe in a collection of posts each 2 be admired for der own personal statement. when in doubt, ask Willy!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-02-2013, 09:33 PM
  2. Replies: 7
    Last Post: 10-17-2012, 10:17 PM
  3. Replies: 3
    Last Post: 01-15-2012, 06:09 AM
  4. Question about functions of string vs char string
    By Robertjh12 in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2011, 03:13 AM
  5. passing pointer to string between functions
    By liats80 in forum C Programming
    Replies: 4
    Last Post: 11-20-2007, 08:30 PM