View Single Post
Old 05-23-2005, 01:22 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
Here is a version, where I've implemented the translate function to take the entire line and convert everything in it, befor returning.
How ever, this introduces some pitfalls, if you have a line of exact 80 chars, and it consist of several words, then this version will have some unexplainable outcome.
This can be solved by using a line like:
Code:
 temp2 = (char*) realloc(temp2, (LINE_LENGTH + (wordcount+1)*strlen(ADDITION))*sizeof(char));
where the value k is reset to the length of the temp2 variable in translate(), and remember to free() it, once we're done.
But since this is just for fun, I dont wanna get into using those things.
Another feature it implements, it will accept vowels that are capital letters, which the privius one didn't.
Code:
/*Pig Latin*/
/*Valerie*/

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

#define LINE_LENGTH 80
#define ADDITION "ay"

int translate(char *trans);


int main()
{
  int i = 0, j = 0, wordcount = 0;
  char line[LINE_LENGTH +strlen(ADDITION)];

  printf("\n\tWelcome to Pig Latin.\n");
  printf("\tPlease enter a sentence you would like to translate\n");
  printf("Real line:\t");
  fflush(stdout);
  fgets(line, LINE_LENGTH, stdin);
  line[strlen(line) -1] = '\0'; /* remove '\n' at the end */
  wordcount = translate(&line);
  printf("In pig Latin:\t%s", line);
  printf("\nNumber of words translated: %d\n", wordcount);
  printf("\nPress enter to exit\n");
  getchar();
  return 0;
}

int translate(char *trans)
{
  char word[LINE_LENGTH +strlen(ADDITION)];
  char temp1[LINE_LENGTH +strlen(ADDITION)];
  char temp2[LINE_LENGTH +strlen(ADDITION)];
  int i = 0, j = 0, k = 0, wordcount = 0;
  while (trans[i]!='\0')
    {
      /* pick out a single word in the line */
      while (trans[i]!= ' ' && trans[i] != '.' && trans[i] != '\0')
        word[j++] = trans[i++];
      ++wordcount;
      word[j] = '\0';
      j = 0;
      {  /* pick out the consonants befor any vowel */
        while(word[j] !='a' && word[j] !='e' && word[j] !='i'
              && word[j] !='o' && word[j] !='u'&& word[j] !='y'
              && word[j] !='A' && word[j] !='E' && word[j] !='I'
              && word[j] !='O' && word[j] !='U'&& word[j] !='Y')
          temp1[j] = word[j++];
        temp1[j] = '\0';
        /* copy the rest of the word */
        while(word[j] != '\0')
          temp2[k++] = word[j++];
        temp2[k] = '\0';
        /* append the consonants to the word */
        strncat(temp2, temp1, strlen(temp1));
        /* append the ay to the end of the word */
        strncat(temp2, ADDITION, strlen(ADDITION));
      }
      /* since we're dealing with the entire line,
       * we let temp2 expand and end up holding every
       * word found in the line
       */
      k = strlen(temp2);
      temp2[k++] = ' ';
      temp2[k] = '\0';
      j = 0;
      if(trans[i] == '.')
        break;
      ++i;
      /* make sure extra spaces aren't screwing it up for us */
      while(trans[i] == ' '|| trans[i] == '\t' || trans[i] == '\r')
        ++i;
    }
  memcpy(trans, temp2, strlen(temp2));
  trans[strlen(temp2)] = '\0';
  return wordcount;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote