69,371
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<string.h>
const char Stop = '#';
const char Symbol_1 = '.';
const char Symbol_2 = '!';
const int MaxLen = 100;
int main(void)
{
char chs[MaxLen];
char Letter;
int Counts = 0;
int Times = 0;
printf("Please Input Words To Test:\n");
while((Letter = getchar()) != Stop)
{
if(Letter == Symbol_1)
{
chs[Counts] = Symbol_2;
++Counts;
++Times;
}
else
if(Letter == Symbol_2)
{
chs[Counts] = Symbol_2;
chs[Counts+1] = Symbol_2;
Counts += 2;
++Times;
}
else
{
chs[Counts] = Letter;
++Counts;
}
}
printf("%d Times\n",Times);
printf("%s\n",chs); //<----这个很奇葩
// int Len = strlen(chs); //<---问题出在这3行代码里
// for(int i = 0 ; i < Len+1 ; ++i)
// printf("%c ",chs[i]);
return 0;
}
void HexDump(char *buf,int len,int addr) {
int i,j,k;
char binstr[80];
for (i=0;i<len;i++) {
if (0==(i%16)) {
sprintf(binstr,"%08x -",i+addr);
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
} else if (15==(i%16)) {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
sprintf(binstr,"%s ",binstr);
for (j=i-15;j<=i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
printf("%s\n",binstr);
} else {
sprintf(binstr,"%s %02x",binstr,(unsigned char)buf[i]);
}
}
if (0!=(i%16)) {
k=16-(i%16);
for (j=0;j<k;j++) {
sprintf(binstr,"%s ",binstr);
}
sprintf(binstr,"%s ",binstr);
k=16-k;
for (j=i-k;j<i;j++) {
sprintf(binstr,"%s%c",binstr,('!'<buf[j]&&buf[j]<='~')?buf[j]:'.');
}
printf("%s\n",binstr);
}
}
#include<stdio.h>
#include<string.h>
const char Stop = '#';
const char Symbol_1 = '.';
const char Symbol_2 = '!';
const int MaxLen = 100;
int main(void)
{
char chs[MaxLen] = {0};/////////// need init, otherwise printf chs will not work correct
int Letter; ///////////////////////// getchar return int not char
int Counts = 0;
int Times = 0;
printf("Please Input Words To Test:\n");
while ((Letter = getchar()) != Stop)
{
if (Letter == Symbol_1)
{
chs[Counts] = Symbol_2;
++Counts;
++Times;
}
else if(Letter == Symbol_2)
{
chs[Counts++] = Symbol_2;
chs[Counts++] = Symbol_2;
++Times;
}
else if ((Letter == EOF) || (Letter == '\n')) //////////////////////in case the wrong input
{//parse input done
break;
}
else
{
chs[Counts] = Letter;
++Counts;
}
}
printf("%d Times\n",Times);
printf("%s\n",chs); //<----Õâ¸öºÜÆæÝâ ---------------------->see line 10
int Len = strlen(chs); //<---ÎÊÌâ³öÔÚÕâ3ÐдúÂëÀï
for(int i = 0 ; i < Len+1 ; ++i)
printf("%c ",chs[i]);
return 0;
}