Thursday, October 8, 2015

This blog is to help student solving Zybook challenging activities of C programming.
Post your Question in the comment section below.



3.9.1 passcode
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>

int main(void) {
   bool hasDigit = false;
   char passCode[50] = "";
   int valid = 0;

   strcpy(passCode, "abc");

   /* Your solution goes here  */
   if(isdigit(passCode[0]) || isdigit(passCode[1])|| isdigit(passCode[2])){
      hasDigit=true;
   }
     


   if (hasDigit) {
      printf("Has a digit.\n");
   }
   else {
      printf("Has no digit.\n");
   }

   return 0;
}

3.9.2 whitespace
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void) {
   char passCode[3] = "";

   strcpy(passCode, "1 ");

   /* Your solution goes here  */
   if(isspace(passCode[0]) && isspace(passCode[1])) {
      passCode[0]='_';
      passCode[1]='_';
  }
   else if(isspace(passCode[0])){
      passCode[0]='_';
   }
   else if(isspace(passCode[1])) {
      passCode[1]='_';
  } 
     



   printf("%s\n", passCode);
   return 0;
}



4.6.1 for loop

#include <stdio.h>

int main(void) {
   int userNum  = 0;
   int i = 0;
   int j = 0;

   /* Your solution goes here  */
for(i=0;i<=userNum;i++){
     
      for(j=1;i>=j;j++){
        printf(" ");
      }
     
      printf("%d\n",i);
   }
   return 0;
}
4.6.2 for loop
#include <stdio.h>

int main(void) {
   int numRows = 2;
   int numCols = 3;

   // Note: You'll need to define more variables
      int i=0;
      int k=0;
      char j='A';
      for(i=1;i<=numRows;i++){
         j='A';
         for(k=1;k<=numCols;k++){
            printf("%d%c ",i,j);
            j++;
         }
      }
   /* Your solution goes here  */

   printf("\n");

   return 0;
}
5.2.2: Printing array elements with a for loop.
#include <stdio.h>

int main(void) {
   const int NUM_VALS = 4;
   int courseGrades[NUM_VALS];
   int i = 0;

   courseGrades[0] = 7;
   courseGrades[1] = 9;
   courseGrades[2] = 11;
   courseGrades[3] = 10;
  for(i=0;i!=NUM_VALS;i++){
     printf("%d ",courseGrades[i]); 
   
  }
   printf("\n");
   for(i=NUM_VALS-1;i>=0;i--){
     printf("%d ",courseGrades[i]); 
   
  }
  printf("\n");

  
 
   /* Your solution goes here  */

   return 0;
}