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

C coding help needed! String manipulation capabilities in BASIC are simple and v

ID: 3838901 • Letter: C

Question

C coding help needed!

String manipulation capabilities in BASIC are simple and very powerful. Let's upgrade the string capabilities in C by developing some of the tools that BASIC lets us use. In this problem, you are to design a portion of the new C string library stringlnit (char* strPtr) Initialize the string to a default value. stringlnitvalue (char* strPtr, char acharString) Initialize the string to the value aCharString. char atPut (char* strPtr, int anIndex, char avalue) Put a value at string index anIndex - use overwrite semantics. char atGet (char* strPtr, int anlndex, char avalue) Return the character at string index anIndex. void atRemove (char* strPtr, int anlndex) Remove the character at string index anIndex - the string reduces by one. Char* subString (char strPtr, char* aSubstringPtr) Return a pointer to the first occurrence of the string pointed to by aSubstringPtr or NULL if the substring is not found. int length (char* strPtr) Return the length of the string. void show(char* strPtr) Display the string.

Explanation / Answer

#include<stdio.h>

void stringInit(char *strPtr){
     if (strPtr !=NULL)
        strPtr = "";
     else
       printf(" Error: NULL pointer");
}

void stringInitValue(char *strPtr, char aCharString){
     if (strPtr !=NULL)
        strPtr = &cCharString;
     else
       printf(" Error: NULL pointer");
}

char atPut(char *strPtr, int anIndex, char aValue){
      char *p;
      p = strPtr;
      int count;
      count = 0;
      if (anIndex < 0){
         printf(" Invalid index");
         return -1;
      }
      if (strPtr != NULL){
         while (*p){
            count++;
            p++;
         }
         if (count < anIndex){
            printf(" Index Out of bound");
            return -1;
         }
         else {
             *(strPtr+anIndex) = aCharString;
             return 0;
         }
      }
      else {
         printf(" Error: NULL pointer");
         return -1;
      }
}

char atGet(char *strPtr, int anIndex, char aValue){
      char *p;
      p = strPtr;
      int count;
      count = 0;

      if (anIndex < 0){
         printf(" Invalid index");
         return -1;
      }
      if (strPtr != NULL){
         while (*p){
            count++;
            p++;
         }
         if (count < anIndex){
            printf(" Index Out of bound");
            return -1;
         }
         else {
             return *(strPtr+anIndex);
            
         }
      }
      else {
         printf(" Error: NULL pointer");
         return -1;
      }
}


void atRemove(char *strPtr, int anIndex){
      char *p;
      p = strPtr;
      int count;
      count = 0;
      int j;

      if (anIndex < 0){
         printf(" Invalid index");
         return;
      }
      if (strPtr != NULL){
         while (*p){
            count++;
            p++;
         }
         if (count < anIndex){
            printf(" Index Out of bound");
            return -1;
         }
         else {
            j = anIndex
            while (j <= count){
              *(strPtr+j) = *(strPtr+j+1);
              j++;
            }
            
         }
      }
      else {
         printf(" Error: NULL pointer");
         return -1;
      }
}

int length(char *strPtr){
      char *p;
      p = strPtr;
      int count;
      count = 0;
      int j;
      if (strPtr != NULL){
         while (*p){
            count++;
            p++;
         }
         return count;
      }
      else {
         printf(" Error: NULL pointer");
         return -1;
      }
}