当前位置: 代码迷 >> C语言 >> 求救,在线等!~
  详细解决方案

求救,在线等!~

热度:253   发布时间:2005-06-10 15:41:00.0
求救,在线等!~
我希望用动态分配做出:输入 : who are you
                                   输出:who
                                             are
                                             you
希望能有人帮帮我。。。

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
void main()
{
char * p,* px;
p=(char *)malloc(sizeof(char));
printf("write your sentence:\n");
if(p=NULL)
{
  printf("error!\n");
}
px=p;
while(1)
{
  scanf("%s",px);
  px++;
  if(px=="\n")
   break;
}
px=p;
while(px)
{
  printf("%s\n",* px);
  px++;
}
}

我做的这个,不懂哪里出错了。
搜索更多相关的解决方案: 在线  

----------------解决方案--------------------------------------------------------
纠正一下,输出的我希望是这样的
who
are
you
----------------解决方案--------------------------------------------------------
错的地方多了
malloc()函数
scanf()函数
我还没有看完
----------------解决方案--------------------------------------------------------
连指针都不会用
还用什么动态分配内存啊
----------------解决方案--------------------------------------------------------
麻烦写一份正确的给我好吗?
谢谢啊,你这样指点我,还是不懂
我初初接触C,还请多多指教
----------------解决方案--------------------------------------------------------

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

main() { char *str,*p; char c;

str=(char*) malloc(30*sizeof(char)); p=str; printf("-------------------------\n"); printf("Input\n"); printf("-------------------------\n"); c=getchar(); *str=c; str++; while((c=getchar())!='\n') { *str=c; str++; } *str='\n'; printf("-------------------------\n\n"); printf("Output\n"); printf("-------------------------\n"); str=p; while(*str!='\n') { if(*str!=' ') printf("%c",*str); else printf("\n"); str++; } getch(); }


----------------解决方案--------------------------------------------------------

//本程序完全可以满足你的要求 //并且两个字符串中间不论有多少空格,都会按你的输出格式输出。 /* 我希望用动态分配做出:输入 : who are you 输出:who are you */

#include <stdio.h> #include <stdlib.h>

#define MAXSTRING 1000 //定义最大的字符个数

char * input(void); void process(char *);

char * input(void) { char *str; str=(char *)malloc(MAXSTRING*sizeof(char)); if(!str) exit(0); puts("please enter the string:"); gets(str); return str; }

void process(char *str) { int flag; flag=0; puts("\noutputing is as following:\n"); while(*str!='\0') { if(*str!=' ') { flag=0; putchar(*str); } else { if(flag==0) { flag=1; putchar('\n'); } } str++; } putchar('\n'); }

int main() { process(input()); return 0; }


----------------解决方案--------------------------------------------------------
呵呵,不错啊,有点长了
----------------解决方案--------------------------------------------------------
非常感谢,我研究研究地。。。
       小弟在这里表示万分感谢了。。。。。。
----------------解决方案--------------------------------------------------------
其他的不说,楼主的
if(p=NULL )表达就有问题呢
要也是(p==NULL)不是吗
----------------解决方案--------------------------------------------------------
  相关解决方案