ECE 209 Example Programs

Piglet Latin

#include <stdio.h>
#include <ctype.h>

int main(void) {
  char c ;
  c = getchar() ;
  while (c != EOF) {
    if (isalpha(c)) {
      char r = c ;
      while(isalpha(c=getchar())) {
        putchar(c) ;
      }
      putchar(r) ;
      putchar('a') ;
      putchar('y') ;
    } else {
      putchar(c) ;
      c = getchar() ;
    }
  }
  return 0 ;
}

Biggest number in input

Doesn't work with empty lines.

#include <stdio.h>

int main(void) {
  int maxNum  ;       /* biggest so far */
  int nextNum ;       /* last number read */
  int numRead = 0 ;   /* number of integer read so far */
  int numReadInLine ; /* number of integer read in last line */
  printf("Looking for the biggest number.\n"
	 "Enter a line that doesn't start"
	 " with a number when you are done\n") ;
  numReadInLine = scanf("%d", &nextNum) ;
  maxNum = nextNum ;
  while (numReadInLine == 1) {
    ++numRead ;
    if (maxNum < nextNum)
      maxNum = nextNum ;
    numReadInLine = scanf("%d", &nextNum) ;
    while (getchar() != '\n') ;
  }
  printf("%d numbers were read.\n", numRead) ;
  if (numRead > 0)
    printf("%d was the largest.\n", maxNum) ;
  return 0 ;
}

Piggy Latin

Doesn't work when the word has a large number of consonants at the beginning.

#include <stdio.h>
#include <ctype.h>

int isvowel(char c) {
  switch(c) {
  case 'a':
  case 'A':
  case 'e':
  case 'E':
  case 'i':
  case 'I':
  case 'o':
  case 'O':
  case 'u':
  case 'U':
    return 1 ;
  default:
    return 0 ;
  }
}

int main(void) {
  char c ;
  c = getchar() ;
  while (c != EOF) {
    if (isalpha(c)) {
      char consonant[7] ;
      int i ;
      int n = 0 ;
      while(! isvowel(c)) {
        consonant[n] = c ;
        ++n ;
        c = getchar() ;
      }
      while(isalpha(c)) {
        putchar(c) ;
        c = getchar() ;
      }
      if (n==0)
        putchar('w') ;
      else
        for (i=0; i<n; ++i)
          putchar(consonant[i]) ;
      putchar('a') ;
      putchar('y') ;
    } else {
      putchar(c) ;
      c = getchar() ;
    }
  }
  return 0 ;
}