记录:C prime plus 字符串的学习!!!
加油 加油 加油!!!!相信自己!!!坚持每一天!!!
#include<stdio.h>
#include<string.h>
#define STLEN 10
#define SIZE 30
#define BUGSIZE 13
#define ANSWER "Grant"
#define STOP "quit"
#define LIM 10
#define LIMS 5
#define TARGSIZE 7
#define LISTSIZE 6
#define MAX 20
#define SIZES 81
#define LIMSS 20
#define HALT " "
/*void put1(const char*);
int puts(const char*);*/
char *s_gets(char *st,int n);
void ststr(char *string[],int int)
int main() //%-6s 6是指占位宽6个字符,-表示输出左对齐!!!知识点
{
/* char car[10]="Tata";
if(car==&car[0]&&*car=='T'&&*(car+1)=='a')
{
printf("Ture");
}
printf("%p",);*/
/*char words[STLEN];
puts("Enter strings (empty line to quite):");
while(fgets(words,STLEN,stdin)!=NULL&&words[0]!='\n')
fputs(words,stdout);
puts("Done.");*/
/*char words[STLEN];
int i;
puts("Enter strings (empty line to quit):");
while(fgets(words,STLEN,stdin)!=NULL&&words[0]!='\n')
{
i=0;
while(words[i]!='\n'&&words[i]!='\0')
i++;
if(words[i]=='\n')
words[i]='\0';
else //如果words[i]=='\0'则执行该代码
while(getchar()!='\n')//【如果代码超过规定长度,那么将会有俩个'\n'出来,一个出现在换fgets里一个出现在输出的结束处】
continue; //【 我们将第一个'\n'换成'\0'来输出规定长度的字符串,用getchar读入但不写入来的方式无效后面的字符串】
puts(words); //最后输出结果,完成一次输出!!!
}
puts("done");*/
/*char side_a[]="Side A";
char dont[]={'W','O','W','!','!','!'};
char side_b[]="Side B";
puts(dont);//dont 不是一个字符串 */
/*自定义输出字符串的函数:void put1(const char *string) //打印字符串,不添加\n!!!
{
while(*string !='\0')
putchar(*string++);
}*/
//实际操作
/*put1("If I'd as much money");
put1("as I could spend,\n");
printf("I count %d characters.\n",put2("I never would cry old chairs to mend")); */
/*char flower[SIZE];
char addon[]="s smell like old shoes.";
puts("What is your favorite flower?");
if(s_gets(flower,SIZE))
{
strcat(flower,addon);
puts(flower);
puts(addon);
}
else
puts("End of file encountered!");
puts("bey");*/
/*char flower[SIZE];
char addon []="s smell like old shoes.";
char bug[BUGSIZE];
int available;
puts("What is your favorite flower?");
s_gets(flower,SIZE);
if((strlen(addon)+strlen(flower)+1)<=SIZE)
strcat(flower,addon);
puts(flower);
puts("What is your favorite bug?");
s_gets(bug,BUGSIZE);
available=BUGSIZE-strlen(bug)-1;
strncat(bug,addon,available);//可以使用的有效长度 ,帅气!!!
puts(bug); */
/*char try[SIZE];
puts("Who is buried in Grant's tomb?");
s_gets(try,SIZE);
while(strcmp(try,ANSWER)!=0)
{
puts("No,that's wrong.Try again.");
s_gets(try,SIZE);
}
puts("That' rigt");*/
/*char input[LIM][SIZE];
int ct=0;
printf("Enter up to %d lines(type quit to quit):\n",LIM);
while(ct<LIM&&s_gets(input[ct],SIZE)!=NULL&&strcmp(input[ct],STOP)!=0)
{
ct++;
}
printf("%d string entered\n",ct);*/
/* const char *list[LISTSIZE]=
{
"astronomy","astounding",
"astrophysics","ostracize",
"asterism","astrophobia",
};
int count =0;
int i;
for(i=0;i<LISTSIZE;i++)
if(strncmp(list[i],"astro",5)==0)
{
printf("Found: %s\n",list[i]);
count++;
}
printf("The list contained %d words beginning" "with astro. \n",count);*/
/*char qwords[LIMS][SIZE];
char temp[SIZE];
int i=0;
printf("Enter %d words beginning with q:\n",LIMS);
while(i<LIMS&&s_gets(temp,SIZE))
{
if(temp[0]!='q')
printf("%s doesn't begin with q!\n",temp);
else
{
strcpy(qwords[i],temp);
i++;
}
}
puts("Here are the words accepted:");
for(i=0;i<LIMS;i++)
{
puts(qwords[i]);
}*/
/*const char *orig="beast";
char copy[SIZE]="Be the best that you can be";
char *ps;
puts(orig);
puts(copy);
ps=strcpy(copy+12,orig);
puts(copy);
puts(ps);*/
/*char qwords[LIMS][TARGSIZE];
char temp[SIZE];
int i=0;
printf("Enter %d words beginning with q:\n",LIMS);
while(i<LIMS&&s_gets(temp,SIZE))
{
if(temp[0]!='q')
printf("%s doesn't begin with q!\n",temp);
else
{
strncpy(qwords[i],temp,TARGSIZE-1);
qwords[i][TARGSIZE-1]='\0';
i++;
}
}
puts("Here are the words accepted:");
for(i=0;i<LIMS;i++)
{
puts(qwords[i]);
}*/
/*char first[MAX];
char last[MAX];
char formal[2*MAX+10];
double prize;
puts("Enter your first name:");
s_gets(first,MAX);
puts("Enter your last name:");
s_gets(last,MAX);
puts("Enter your prize money:");
scanf("%lf",&prize);
sprintf(formal,"%s,%-19s:$%6.2f\n",last,first,prize);
puts(formal);*/
char input[LIMSS][SIZES]; //存储输入的数组
char *ptstr[LIMSS]; //内含指针变量的数组
int cr=0,k; //输入计数,与输出计数
printf("Input up to %d lines, and I will sort them.\n",LIM);
printf("To stop , press the Enter key at a line's start.\n");
while(ct<LIM&&s_gets(input[ct],SIZE)!=NULL&&input[ct][0]!='\0')
{
ptstr[ct]=input; //设置指针指向字符串
ct++;
}
st
}
/*1.
void put1(const char *string)
{
while(*string)//与*string != '\0' 相同
putchar(*string++);
}
int put2(const char *string)
{
int count =0;
while(*string)
{
putchar(*string++);
count++;
}
putchar('\n');
return(count);
}*/
char *s_gets(char *st,int n)
{
char *ret_val;
int i=0;
ret_val=fgets(st,n,stdin);
if(ret_val)
{
while(st[i]!='\n'&&st[i]!='\0')
i++;
if(st[i]=='\n')
st[i]='\0';
else
while(getchar()!='\n')
continue;
}
return ret_val;
}