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

Need to fix code so follows these guidelines. http://www.eecsdx.com/linuxc/bin_s

ID: 644148 • Letter: N

Question

Need to fix code so follows these guidelines. http://www.eecsdx.com/linuxc/bin_str-%E9%80%92%E5%BD%92%E8%B0%83%E7%94%A8/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* convertString(char *c,int len);
int main(int argc,char *argv[])
{
   int i,length;
   char *cs;

   length=strlen(argv[1]);

   cs=convertString(argv[1],length);

   for(i=0;i<length;i++) // ?
       printf("%c", cs[i]);
   //getch()
   return 0;
}

char* convertString(char *c,int len)
{
   char* newchar,newchar1;
   int i,count = 0;

   printf(" ");

   for(i=0;i<count;i++)
   {
       if(c[i]=='0'||c[i]=='1')
       {
           newchar[i]=c[i];
       }
       if(c[i]=='x')
       {
           if(count==0)
           {
               newchar[i]='0';
           }
           if(count==1)
           {
               newchar[i]='1';
           }
       }
   }
   printf(" ");
   for(i=0;i<count;i++) // ?
   {
       printf("%c",newchar[i]);
   }
   count++;

   if(count==1)
   {
       return(convertString(c,len));
   }
   return newchar;
}

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

void convertString(char *str, int len, int i)
{
   if(i >= len)
       return;

   if(str[i] == '0' || str[i] == '1')
       printf("%c", str[i]);  
   else
       printf("%d", rand() % 2);

   convertString(str, len, i+1);
}

int main(int argc,char *argv[])
{
   srand(time(NULL));

   char *str = "x1x0";
   int len = strlen(str);

   printf("Original: %s ", str);
   printf("Random result: ");
   convertString(str, len, 0);
   printf(" ");
  
   return 0;
}

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