Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function alter_string that creates a dest string which is the source str

ID: 3545764 • Letter: W

Question

Write a function alter_string that creates a  dest string which is the source string with the following changes:

A

Write a function alter_string that creates a dest string which is the source string with the following changes: A '%' is inserted BEFORE the 6th character of the source string A '* ' is inserted AFTER the 9th character of the source string A '?' is inserted AFTER the 5th to the last character of the source string Assume the input source into the function is a string which is of length greater than or equal to 14 so that the ninth character and the fifth to the last character are SEPARATE characters i. e. Write a main program to test the function alter_str. with the inputs in the above table - run the program three times with each of the above inputs. The main program should ask the user for an input string and then call the function and then print out both the source and destination string.

Explanation / Answer

#include <stdio.h>

void alter_str(char *dest, const char *source);

int main(void)
{
    char source[100];
    char dest[100];
   
    printf("Input string: ");
    scanf("%s", source);
   
    alter_str(dest, source);
   
    printf("Source string: %s ", source);
    printf("Destination string: %s ", dest);
    return 0;
}


void alter_str(char *dest, const char *source)
{
    int i, j, charLeft;
    for (i = 0, j = 0; i < 5; ++i, ++j) dest[j] = source[i];
    dest[j++] = '%';
    for ( ; i < 9; ++i, ++j) dest[j] = source[i];
    dest[j++] = '*';
    charLeft = 0;
    while (source[i+charLeft]) ++charLeft;
    for ( ; charLeft >= 5; ++i, ++j, --charLeft) dest[j] = source[i];
    dest[j++] = '?';
    for ( ; source[i]; ++i, ++j) dest[j] = source[i];
    dest[j] = '';
}


//test run: http://ideone.com/seutIx

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote