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

I dont know what is wrong with my code with the function Write the string functi

ID: 3553815 • Letter: I

Question

I dont know what is wrong with my code with the function


Write the string function TrStr. This function takes a first string of arbitrary content, and a second "translation table" string of character pairs, guaranteed to have an even number of chars. Each char pair in the translation table string indicates a substitution to make in the first string, replacing the first character in the pair with the second. For instance, a translation table string of "aAeEiIoOuU" indicates that all lower case vowels should be capitalized in the first string. If the same char appears several times in the translation table, use the first occurrence. Do not use any string library functions; write TrStr from scratch.



#include <stdio.h>

#define strchr
#define strlen

void TrStr(char *str1, char *str2);
void TrStr(char *str1, char *str2)
{
   while(*str1++) {
      while(*str2++) {
               if(*str1 == *str2) {
               printf("%c", *str2);
               *str1 = *(str2+1);   
               }
      }
   }  
}
int main() {
   char str1[20], str2[20], *test = "";

   TrStr(test, "abcd");
   printf("Empty translates to |%s| ", test);

   while (EOF != scanf("%s %s", str1, str2)) {
      TrStr(str1, "");
      printf("%s -> ", str1);
      TrStr(str1, str2);
      printf("%s ", str1);
   }
  
   return 0;
}

Explanation / Answer

//use This Function


void TrStr(char *str1, char *str2)

{

while(*str1++)

{

int i=0;

for(;str2[i]!='';i+=2)

{

if(str2[i]==*str1)

{

*str1 = str2[i+1];

break;

}

}

}

}