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

I need help with writing the following string functions in C please provide step

ID: 3762263 • Letter: I

Question

I need help with writing the following string functions in C please provide steps. Thanks alot!

(1) char **lengthSplit(const char *s, int n) -

this function takes a string and splits it up to an array of strings. The split will be length-based: the function will also take an integer n and will split the given string up into strings of length n. It is possible that the last string will not be of length n. You will not need to communicate how large the resulting array is as the calling function knows the string length and n. For example, if we pass “Hello World, how are you?” with n = 3 then it should return an array of size x containing the strings “Hel”, “lo ”, “Wor”, “ld,”, “ ho”, “w a”, “re ”, “you”, “?”. 1

(2) char * flipCase(const char *src) -

this function creates a copy of a given string and then flips the case of each character: upper case letters should become lower case and vice versa. For example, the string “GNU Image Processing Tool-Kit” should become “gnu iMAGE pROCESSING tOOLkIT.”

(3) int numberOfTokens(char *str, char d) -

this function will find the number of tokens in a string. It returns the number of tokens in the string str while using the delimiter d.

Explanation / Answer

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

char **lengthSplit(const char *s, int n){

char **arr;

int index=0;

int i, total, len;

len = strlen(s);

total = len/n + (len%n == 0 ? 0 : 1);

arr = (char **)malloc(sizeof(char *)*total);

  

for (i=0; i<len; i=i+n) {

arr[index] = (char *)malloc(sizeof(char)*n);

strncpy(arr[index], &s[i], n);

index++;

}

return arr;

}

char * flipCase(const char *src) {

char *arr;

int i;

int len = strlen(src);

arr = (char *)malloc(sizeof(char)*len);

for (i=0; i<len; i++) {

if (src[i] >= 'a' && src[i] <='z') {

arr[i] = 'A' + (src[i]-'a');

}

else if (src[i] >= 'A' && src[i] <='Z') {

arr[i] = 'a' + (src[i]-'A');

}

else {

arr[i] = src[i];

}

}

return arr;

}

int numberOfTokens(char *str, char d) {

int i, number=0;

int len = strlen(str);

for (i=0; i<len; i++) {

if (str[i] == d) {

number++;

}

}

return number+1;

}

int main() {

char **arr;

int i, n, len;

  

char *a = "Hello World, how are you?";

char *a1 = "GNU Image Processing Tool-Kit";

n=3;

arr=lengthSplit(a, n);

len=strlen(a);

for (i=0; i<len/n + (len%n == 0 ? 0 : 1); i++) {

printf("%s ", arr[i]);

}

  

printf(" Flipside of %s is: %s", a1, flipCase(a1));

  

printf(" Number of tokens in %s with delim - is: %d", "arka-prava-de", numberOfTokens("arka-prava-de", '-'));

  

  

return 0;

}