11.23下午

本文通过示例代码展示了C++中字符串的获取、拼接、检查及输入输出等基本操作,包括自定义函数`s_gets`用于安全读取用户输入,并通过`fgets`、`strcat`、`strncat`等函数处理字符串。文章还涵盖了字符串长度限制、错误检查以及字符串比较等关键知识点。
摘要由CSDN通过智能技术生成

记录: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;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值