Function (split_string_at_index) given string & index , can't figure out how pass back to main after splitting into source_left and right



  • #include <stdbool.h>
    
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #include <string.h>
    
    #define MAX_SUBSTRING 15
    
    // Helper function for determining if a string validly expresses a non-negative integer.
    
    // Feel free to use where needed (and to study it as an example).
    
    bool is_non_negative_numeral(char* numeral) {
        int numeral_length = strlen(numeral);
        for (int i = 0; i < numeral_length; i++) {
        if (numeral[i] < '0' || numeral[i] > '9') {
        return false;
        }
    }
    
    return true;
    }
    
    // ***** IMPLEMENT ME! *****
    
    char* split_string_at_index(char* string, int index, char* left, char* right) {
    
        right = &string[index];
        char source_left[20] = "";
        strncat (source_left,string, index);
        printf("[%s] [%s]\n", source_left, right);
    
    }
    
    int main(int argc, char** argv) {
    // Check for the correct number of arguments.
    if (argc != 3) {
    printf("This program requires exactly two command line arguments:\n");
    printf("The string to split and the index at which to split it.\n");
    return 1;
    }
    
       char* string_to_split = argv[1];
       char* index_string = argv[2];
    // Check that the second argument is in the correct format.
    
    if (!is_non_negative_numeral(index_string)) {
    printf("%s is not a valid index.\n", index_string);
    return 1;
    }
    char left[MAX_SUBSTRING + 1];
    
    char right[MAX_SUBSTRING + 1];
    int string_length = strlen(string_to_split);
    
    printf("The string length as in integer equals %d\n",string_length);
    
    int index = atoi(index_string);
    
    // test printf(" The index inside of atoi is %d\n", index);
    // test printf(" The index_string inside of atoi is %s\n",index_string);
    
    // Check that the given index is within the correct range (two conditions for this---hope you can see why).
    if (index < 0 || index > string_length) {
    printf("The index %d out of range.\n", index);
    return 1;
    }
    if (index > MAX_SUBSTRING || string_length - index > MAX_SUBSTRING) {
    printf("Splitting at %d will exceed the limit of %d characters per substring.\n", index, MAX_SUBSTRING);
    return 1;
    }
    // Now that we have validated the user’s input, we can call the function.
    
    split_string_at_index(string_to_split, index, left, right);
    printf("[%s] [%s]\n", left, right);
    return 0;
    
    }
    
    


  • Thank you. I think I was over complicating the solution, focusing on pointers, dereferencing and parameter passing.
    After a good night's sleep, I came up with the following solution:

    strncpy(left,string,index);
    strcpy(right,&string[index]);

    That's all it took to get the job done.



  • Do you mind giving us some context? What does the problem suppose to do for example? That will save the person who is trying to help you a considerable amount of time. Thank you.



  • The program takes 2 cmd line args , a string to split and an index at which to split it.
    It calls a function to do the split:
    split_string_at_index(string_to_split, index, left, right);
    I created a function to do the split:

    char* split_string_at_index(char* string, int index, char* left, char* right) {
    
        right = &string[index];
        char source_left[20] = "";
        strncat (source_left,string, index);
        printf("[%s] [%s]\n", source_left, right);
    return 0;
    }
    

    I am confused about how I can pass parameters , left & right, back to main to print them out. Right now, I'm returning 0 because I tried returning different values but all of my attempts failed.
    Right now if I execute the program, the string is split and printed out inside of the function but, I am unable to figure out how to pass left and right back to main to print from there.
    Currently, if I execute my program, I get the following output:

     ./test.out splitme 5
    [split] [me]
    [] []
    


  • Thanks for the clarification!

    OK. I am not a C developer but I think I figured it out through pure luck. I do not recommend taking my answer as the best way to solve the problem but it works. try the following:

    char* split_string_at_index(char* string, int index, char* left, char* right) {
        char source_left[20] = "";
        strncat (source_left,string, index);
        char source_right[20] = "";
        strcpy(source_right, &string[index]);
    
        size_t length = strlen(source_left);
        size_t i = 0;
        for (; i < length; i++) {
            left[i] = source_left[i];
        }
    
        size_t length_right = strlen(source_right);
        size_t j = 0;
        for (; j < length_right; j++) {
            right[j] = source_right[j];
        }
    }
    

    Hope this helps 🤞



  • Thank you. I think I was over complicating the solution, focusing on pointers, dereferencing and parameter passing.
    After a good night's sleep, I came up with the following solution:

    strncpy(left,string,index);
    strcpy(right,&string[index]);

    That's all it took to get the job done.



  • Oh nice. Thanks for letting us know. Do you mind selecting your solution as the correct one? Thanks. Post anytime.



  • Thanks again and I will do that.


Log in to reply