[ create a new paste ] login | about

Link: http://codepad.org/60lHL7wU    [ raw code | output | fork ]

C, pasted on Jul 18:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFSIZE 3 /* >= 2 */
char *getline(void)
{
  static char inbuff[BUFFSIZE];
  char *outbuff_malloc, *tmpbuff;
  char *p;
  int fEOL;

  if ((outbuff_malloc = malloc(1)) == NULL)
    return NULL;
  *outbuff_malloc = '\0';
  fEOL = 0;
  do {
    if (fgets(inbuff, BUFFSIZE, stdin) == NULL)
      break;
    for (p = inbuff; *p != '\0'; p++)
      ;
    if (*(p - 1) == '\n') {
      *(p - 1) = '\0';
      fEOL = 1;
    }
    if ((tmpbuff = realloc(outbuff_malloc, strlen(outbuff_malloc) + strlen(inbuff) + 1)) ==NULL) {
      free(outbuff_malloc);
      return NULL;
    }
    strcat(tmpbuff, inbuff);
    outbuff_malloc = tmpbuff;
  } while (!fEOL);
  return outbuff_malloc;
}

int main() {
  char *s1, *s2;
  int l1, l2;
  printf("input string 1: ");
  s1 = getline();
  printf("input string 2: ");
  s2 = getline();
  l1 = strlen(s1);
  l2 = strlen(s2);
  if (l1 > l2)
    printf("string1 is longer than string2.(%d)\n", l1 - l2);
  else if (l2 > l1)
    printf("string2 is longer than string1.(%d)\n", l2 - l1);
  else
    printf("string1 and string2 is same length.\n");
  free(s1);
  free(s2);
  return 0;
}
/* end */


Output:
1
input string 1: input string 2: string1 and string2 is same length.


Create a new paste based on this one


Comments: