Thread: How to Interpolate between Strings?

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    3

    How to Interpolate between Strings?

    I have to write a function that prints out letters alternating from input_1 and input_2. Here is the body of the code. I know that I have to return a pointer from an array, but I don't know how to begin this. Would I use a for loop alternating between characters?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_LENGTH 10
    
    
    char * interpolate(char * a, char * b);
    
    
    int main(void) {
        char input_1[MAX_LENGTH + 1];
        char input_2[MAX_LENGTH + 1];
        scanf("%s %s", input_1, input_2);
    
    
        char * answer = interpolate(input_1, input_2);
        printf("First String = %s\n", input_1);
        printf("Second String = %s\n", input_2);
        printf("Answer String = %s\n", answer);
        free(answer);
        return 0;
    }
    
    
    char * interpolate(char * a, char * b) {
    //MY CODE GOES HERE

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Quote Originally Posted by LP8914 View Post
    I have to write a function that prints out letters alternating from input_1 and input_2.
    It looks like you have to create a new string, not just print the result. Have you learned about dynamic memory allocation?

    Quote Originally Posted by LP8914 View Post
    Would I use a for loop alternating between characters?
    That might be worth exploring. Have you tried it?

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    3
    just made my function look like this. But I'm still not sure how I would go about alternating. Wouldn't even know where to begin with my for loop.

    Code:
    char * interpolate(char * a, char * b) {
    char *answer;
    
    
    answer = malloc(strlen(a) + strlen(b) + 1);
    
    
    strcpy(answer, a);
    strcat(answer, b);
    return answer;

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    If string 1 is "cat", and string 2 is "saw", what would the result look like?

  5. #5
    Registered User
    Join Date
    Dec 2016
    Posts
    3
    "catsaw"

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    That is not "printing out letters alternating from input_1 and input_2", nor is it really string interpolation as I understand it.

    In order to get the output you described, though, look at the relationship between that and the input strings. The first letter of the output is the first letter of input 1. The second letter of the output is the second letter of input 1. Find the pattern, and come up with a list of logical steps to achieve the results you want. If you get stuck, show us what you've come up with and we can go from there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing Python strings to C strings
    By hinesro in forum C Programming
    Replies: 0
    Last Post: 11-29-2015, 09:14 PM
  2. Replies: 2
    Last Post: 05-16-2012, 06:08 AM
  3. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  4. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  5. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM

Tags for this Thread