• 字符串的使用


    1.字符串常量
     char *name="sjsj";
    系统分配内存 字长+1 要加''
    2.
     char *address=NULL;
     address=(char *)malloc(10*sizeof(char));
     if(address==NULL){
     exit(EXIT_FAILURE);
     }
     scanf("%s",address);碰见空格默认结束
     //realloc重新分配内存空间 将原来的内存中的内容复制 然后在其后加内存(用于内存不够时)
     address=(char *)realloc(address, 12*sizeof(char));
     if(address==NULL){
     exit(EXIT_FAILURE);
     }
     
     //只要是malloc realloc 分配内存空间 必须手动释放
     free(address);
     数组和指针的区别:
     1.定义数组 系统会自动为这个数组分配内存空间
     2.指针方式 需要自己手动申请内存空间 malloc realloc calloc
     需要自己去释放内存 free
     数组和指针可以相互使用
     */
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    int length(char*a,int n);
    int main(int argc, const char * argv[]) {
        
        int count = 1;
        char *string = NULL;
      string=  (char*)malloc(1*sizeof(char));
        if (string == NULL) {
            exit(EXIT_FAILURE);
        }
        printf("请输入:
    ");
        
        while(1){
            char c = getchar();
            //判断是否结束
            if (c == '
    ') {
                string[count-1]='';
                break;
            }
           string= realloc(string, count+1);
            if (string == NULL) {
                free(string);
                exit(EXIT_FAILURE);
            }
            string[count -1] = c;
            
            count++;
        }
        
     int m=   length(string,count);
        printf("%d",m);
        return 0;
    }
    // i am a student
    int length(char*a,int n)
    {
        int number = 0;
        bool start = false;//单词状态 开始或结束
        for(int i = 0;a[ i ]!='';i++)
        {
            char c=a[ i ];
            if ((c >='a'&&c<='z')||(c<='Z'&&c>='A')) {
                if (start == false) {
                    number++;
                    start =true;
            }
            }else{
                if (start == true) {
                    start = false;
                }
            }
        }
        return number;
    }
  • 相关阅读:
    Powershell 音乐播放
    Powershell指令集_2
    Powershell指令集_2
    Powershell 邮件发送
    Powershell 邮件发送
    Oracle 11g 关闭内存自动管理
    Oracle 11g 内存手动管理
    Oracle 内存参数调整
    RESTful三理解
    RESTful三理解
  • 原文地址:https://www.cnblogs.com/kinghyt/p/10159484.html
Copyright © 2020-2023  润新知