65,208
社区成员
发帖
与我相关
我的任务
分享#include <stdio.h>
#define len 10
int main(int argc, char **argv){
int i = 0, length = len;
char c;
char *p = (char *)malloc(sizeof(char) * length);
memset(p, 0, length);
while((c = getchar()) != '\n'){
p[i++] = c;
getchar();
if(i == length){
length += len;
char * tmp = (char *)malloc(sizeof(char) *length);
memset(tmp, 0, length);
memcpy(tmp, p, length - len);
free(p);
p = tmp;
}
}
printf("string length = %d\n", i);
printf("context is:%s\n", p);
return 1;
}
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LEN 10
int main() {
int i, length;
char c;
char *p,*tmp;
length=LEN;
p = (char *)malloc(sizeof(char) * length);
if (NULL==p) return 1;
i=0;
while((c = getchar()) != '\n') {
p[i++] = c;
getchar();
if (i == length){
length += LEN;
tmp = (char *)realloc((void *)p,sizeof(char) *length);
if (NULL==tmp) {free(p);return 1;}
p = tmp;
}
}
p[i]=0;
printf("string length = %d\n", i);
printf("context is:%s\n", p);
free(p);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LEN 10
int main() {
int i, length;
char c;
char *p,*tmp;
length=LEN;
p = (char *)malloc(sizeof(char) * length);
if (NULL==p) return 1;
i=0;
while((c = getchar()) != '\n') {
p[i++] = c;
getchar();
if (i == length){
length += LEN;
tmp = (char *)realloc((void *)p,sizeof(char) *length);
if (NULL==tmp) {free(p);return 1;}
p = tmp;
}
}
p[i]=0;
printf("string length = %d\n", i);
printf("context is:%s\n", p);
return 0;
}
string s;
while(cin>>s)
{
//....ctl+c结束
}