#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    How to obtain substring from full string? Please Look.


    There are many limitations placed for this code. I am still learning so I need hints for this assignment.

    No extra variables, no macros or functions other than GetSubstring can be called.
    Main function cannot be changed.

    This is my code. Focus on the GetSubstring function part.

    #include <stdio.h>
    #include <stdlib.h>
    char *GetSubstring(const char source[], int start, int count, char result[]);

    int main(void)
    {
    const char source[] = "one two three";
    char result[] = "123456789012345678";

    puts(GetSubstring("This is really fun", 2, 800, result));
    puts(GetSubstring("This is really fun", 261, 9, result));
    puts(GetSubstring("This is really fun", 0, 12, result));
    puts(GetSubstring(source, 5, 87, result));
    puts(GetSubstring(source, 18, 7, result));
    puts(GetSubstring(source, 6, 5, result));
    puts(GetSubstring(source, 0, 3, result));

    return(EXIT_SUCCESS);
    }


    char *GetSubstring(const char source[], int start, int count, char result[])
    {
    char *copy = result;
    for (; *source != '\0' || *source == source[start]; start--)
    {
    while (*source != '\0')
    {
    *result++ = *source++;
    }

    }

    *result += '\0';
    return (copy);
    }


    Current Output is:

    This is really fun
    This is really fun
    This is really fun
    one two threey fun
    one two threey fun
    one two threey fun
    one two threey fun

    Process returned 0 (0x0) execution time : 0.082 s
    Press any key to continue.
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,552
    Rep Power
    2337
    Code:
    *result += '\0';
    Can't do this with C strings.

    result is already to pointing to where you need to put the null terminator, so just use =.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    right.


    Thanks for that I'm trying everything I can to change things around
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,413
    Rep Power
    1871
    1. GetSubstring doesn't use the count parameter. Use it, or remove it.

    2. Massive out of range values
    puts(GetSubstring("This is really fun", 2, 800, result));
    puts(GetSubstring("This is really fun", 261, 9, result));
    If you use these to index your short strings, then you have undefined behaviour in your code.

    3. When you post code, make sure you put [code][/code] tags around the code. It should look like this.
    Code:
    int main(void)
    {
        const char source[] = "one two three";
        char result[] = "123456789012345678";
    
        puts(GetSubstring("This is really fun", 2, 800, result));
        puts(GetSubstring("This is really fun", 261, 9, result));
    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

IMN logo majestic logo threadwatch logo seochat tools logo