|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
//用二维数组存放每个单词
#include <stdio.h>
#define MAX 80
int main(void)
{
char str[MAX+1];
char *p=str;
int cot=0,i=0,j=0;
char ch;
fgets(str,MAX,stdin);
while(*p++!='\n')
{
if(*p==' ')
{
cot++;
}
}
char word[cot][30];
p=str;
while((ch=*p++)!='\n')
{
if(ch!=' ')
{
word[i][j++]=ch;
}
if(ch==' ')
{
word[i][j]='\0';
i++;
j=0;
}
}
word[i][j]='\0';
for(cot;cot>=0;cot--)
{
printf("%s",word[cot]);
if(cot>0)
{
printf(" ");
}
}
return 0;
}
我的思路是先创建一个二维数组,存好每个单词,最后根据最后一个单词没有句号输出,但是结果说我的程序运行时出现错误(如数组越界等问题),这是哪里出现了问题呢,请教各位大佬!
本帖最后由 jhq999 于 2022-3-18 07:40 编辑
- #include <stdio.h>
- #define MAX 80
- int main(void)
- {
- char str[MAX+1];
- char *p=str;
- int cot=0,i=0,j=0;
- char ch;
-
- fgets(str,MAX,stdin);
- while(*p++!='\n')
- {
- if(*p==' ')
- {
- cot++;
- }
- }
- cot++;//最后一个没有空格
- char (*word)[30]=new char[cot][30];
- p=str;
- while((ch=*p++)!='\n')
- {
- if(ch!=' ')
- {
- word[i][j++]=ch;
- }
- if(ch==' ')
- {
- word[i][j]='\0';
- i++;
- j=0;
- }
- }
- word[i][j]='\0';
-
- for(cot;cot>0;cot--)///////////
- {
- printf("%s",word[cot-1]);
- if(cot>0)
- {
- printf(" ");
- }
- }
- return 0;
- }
复制代码- #include <stdio.h>
- #define MAX 80
- int main(void)
- {
- char str[MAX+1];
- int cot=0,i=0,j=0;
- char ch;
-
- fgets(str,MAX,stdin);
- while(str[cot]!='\n')
- {
- cot++;
- }
- str[cot]='\0';
- for(cot;cot>0;cot--)
- {
- if (str[cot]==0x20)
- {
- printf("%s",str+cot+1);
- printf("%c",0x20);
- str[cot]='\0';
- }
- }
- printf("%s",str);
- return 0;
- }
复制代码- w eqrty u iop f gggh kl a
- a kl gggh f iop u eqrty w
复制代码
|
-
问题
|