Skip to content

Instantly share code, notes, and snippets.

@giuscri
Created November 18, 2014 12:06
Show Gist options
  • Save giuscri/cf73217e827dde007bd4 to your computer and use it in GitHub Desktop.
Save giuscri/cf73217e827dde007bd4 to your computer and use it in GitHub Desktop.
ll
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *scanner();
void my_realloc(char **str,int size);
int main(void){
int size=100,i=0;
//scanf("%d",&n);
char **str,*p;
str=malloc(size);
while(1){
if(i>=size){
my_realloc(str,size);
}
p=scanner();
int j = 0;
for (j = 0; j < 4; j++) {
printf("%c", p[j]);
}
str[i++]=p;
if(strcmp(str[i],"\n")==0)
break;
}
return 0;
}
void my_realloc(char **str, int size){
size*=2;
str=realloc(str,size);
if(str==NULL)
exit(0);
}
char *scanner(){
char *p, ch;
int n=0,size=2;
p=malloc(size);
while(1){
ch=getchar();
if(size>=n){
size*=2;
p=realloc(p,size);
}
if(ch==' ' || ch=='\n'){
p[n]='\0';
break;
}
p[n++]=ch;
}
return p;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment