【用while输入数组有问题 请大家帮忙】

WYD2608 2011-11-14 10:59:05
#include "stdio.h"
#include "string.h"
#include "memory.h"
main()
{
int i=0,j=0,nLen;
char *p,*q,str[]="My time is limited.",words[100][20];
memset(words,'\0',sizeof(words));
p=str;
while (*p)
{
if (*p==' ' || *p=='!' || *p==',' || *p=='.' || *p=='?')
{
words[i++][j]='\0';
j=0;
}else
{
if (j==0 && (*p>='a' && *p<='z'))
{
words[i][j++]=*p - 32;
}else
{
words[i][j++]=*p;
}
}
p++;
}
p=words[0];
//就从这里下面这段 大家帮忙
while (*p)
{
printf("%s\n",p+i);
}
}
...全文
287 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
JoeBlackzqq 2011-11-14
  • 打赏
  • 举报
回复
参考下我的代码吧


#include <stdio.h> // 标准库中的头文件建议用<>
#include <string.h>
//#include "memory.h"

main()
{
int i=0,j=0,nLen;
char *p,*q,str[]="My ti* me is! limi#ted.",words[100][20];
memset(words,'\0',sizeof(words));
p=str;
while (*p)
{
//if (*p==' ' || *p=='!' || *p==',' || *p=='.' || *p=='?')
if(!(isalpha(*p) || isdigit(*p))) // 不是字母也不是数字,则为一个单词的结束
{
if(j > 0) // 当前单词长度不为空
{
words[i++][j]='\0';
j=0;
}
}
else
{
if (j==0 && (*p >= 'a' && *p <= 'z'))
{
words[i][j++]=*p - 32; // 将单词首字母变成大写
}
else
{
words[i][j++]=*p;
}
}
p++;
}
//p=words[0];
////就从这里下面这段 大家帮忙
//while (*p)
//{
// printf("p: %x\t", p);
// printf("%s\n", p++);
//}
i = 0;
while (strlen(words[i]) > 0)
{
printf("%d: %s\n", i, words[i]);
i++;
}
}

JoeBlackzqq 2011-11-14
  • 打赏
  • 举报
回复
。。。。。。。。。。。。。
i = 0;
while (strlen(words[i]) > 0)
{
printf("%d: %s\n", i, words[i]);
i++;
}
xiejijun_05 2011-11-14
  • 打赏
  • 举报
回复
      
while (*p++)
{
printf("%c\n",p);
}

xiejijun_05 2011-11-14
  • 打赏
  • 举报
回复
while (*p++)
{
printf("%c\n",*p);
}
xiejijun_05 2011-11-14
  • 打赏
  • 举报
回复

while (*p++)
{
printf("%s\n",p+i);
}
霸气傲中原 2011-11-14
  • 打赏
  • 举报
回复
数组指针是这样的
char *p,*q,str[]="My time is limited.",words[100][20];
char (*p)[20];
p=words;
WYD2608 2011-11-14
  • 打赏
  • 举报
回复
我想明白了

是要用一维指针数组
JoeBlackzqq 2011-11-14
  • 打赏
  • 举报
回复

#include <stdio.h> // 标准库中的头文件建议用<>
#include <string.h>

main()
{
char *t[20]; // 声明一个指针数组,长度为20
char (*t2)[20]; // 声明一个指针,该指针指向含20个元素的数组
printf("%d: %x, %x\n", sizeof(t), t, t+1);
printf("%d: %x, %x\n\n", sizeof(t2), t2, t2+1);
//return 0;

int i=0,j=0,nLen;
char *p,*q,str[]="My ti* me is! limi#ted.",words[100][20];
memset(words,'\0',sizeof(words));
p=str;
while (*p)
{
//if (*p==' ' || *p=='!' || *p==',' || *p=='.' || *p=='?')
if(!(isalpha(*p) || isdigit(*p))) // 不是字母也不是数字,则为一个单词的结束
{
if(j > 0) // 当前单词长度不为空
{
words[i++][j]='\0';
j=0;
}
}
else
{
if (j==0 && (*p >= 'a' && *p <= 'z'))
{
words[i][j++]=*p - 32; // 将单词首字母变成大写
}
else
{
words[i][j++]=*p;
}
}
p++;
}

// 法一:用p
p=words[0];
while (strlen(p) > 0) // 用p
{
printf("p: %x\t%s\n", p, p);
p += 20;
}
printf("\n");

// 法二:用p2
char (*p2)[20] = words;
while (strlen((const char*)p2) > 0) // 用p2
{
printf("p2: %x\t%s\n", p2, p2);
p2++;
}
printf("\n");

//法三:用words
i = -1;
while (strlen(words[++i]) > 0) // 用words
{
printf("wrods[%d]: %s\n", i, words[i]);
}
}



编译输出:

[zcm@t #109]$make
gcc -g -c -o a.o a.c
gcc -g -o a a.o
[zcm@t #110]$./a
80: bfb5d554, bfb5d558
4: 8049a08, 8049a1c

p: bfb5cd6c My
p: bfb5cd80 Ti
p: bfb5cd94 Me
p: bfb5cda8 Is
p: bfb5cdbc Limi
p: bfb5cdd0 Ted

p2: bfb5cd6c My
p2: bfb5cd80 Ti
p2: bfb5cd94 Me
p2: bfb5cda8 Is
p2: bfb5cdbc Limi
p2: bfb5cdd0 Ted

wrods[0]: My
wrods[1]: Ti
wrods[2]: Me
wrods[3]: Is
wrods[4]: Limi
wrods[5]: Ted
[zcm@t #111]$
WYD2608 2011-11-14
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 joeblackzqq 的回复:]
参考下我的代码吧


C/C++ code


#include <stdio.h> // 标准库中的头文件建议用<>
#include <string.h>
//#include "memory.h"

main()
{
int i=0,j=0,nLen;
char *p,*q,str[]="My ti* me is! limi#t……
[/Quote]

您这样是可以正确输出的

那么 不能用二维数组指针了么?

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧