Archive for March 1, 2012

TEST1 – 20113 Q1

Posted: March 1, 2012 in Uncategorized
#include <cstdio>


void concat(int no, char** file) {
  FILE* fp1 = fopen(file[1], "w");
  FILE* fp2;      
  int i;
  char ch;

  for(i = 2; i < no ; i++){
    fp2 = fopen(*(file+i), "r");
    while((ch=fgetc(fp2))!=EOF){
      fputc(ch, fp1);
    }
    fclose(fp2);
  }
  fclose(fp1);
}

    
int main(int argc, char** argv) {
  if(argc < 3)
    printf("Usage: %s DestFile SrcFile1 [SrcFile2 SrcFile3 ...]<ENTER>\n", *argv);
  else
    concat(argc, argv);
  return 0;
}

COLOR Console

Posted: March 1, 2012 in Uncategorized

Test 1 – 20113 Q2 and Q3

Posted: March 1, 2012 in Uncategorized
#include <cstdio>
#include <cstring>



//20113 Answer to question 2
void getStr(char *str, int (*IsValid)(const char*), const char* prompt){
  int done = false;
  while(!done){
    printf(prompt);
    scanf("%s", str);
    if(IsValid && IsValid(str)){
      done = 1;
      printf("Hello! yourphone number is: %s\n", str);
    }
  }
}

//20113 Answer to question 3
int ValidPhone(const char* st){
  int flag = true;
  int i;
  if (strlen(st)!=10) flag = false;
  for(i = 0; flag && st[i] != '\0' && st[i] >= '0' && st[i] <= '9'; i++);
  if (!flag && i != 10) flag = false;
  return flag;
}

int main() {
  char string[80];
  getStr(string, ValidPhone, "Please enter your phone: ");
  return 0;
}