米缸虫

导航

13.4练习题5

#include<stdio.h>
#include<ctype.h>

#define  LEN  20

void capitalize(char a[],int LEN+1 );

int main()
{
    int i;
    char a[LEN+1] = "Asawdw1123,.";
    capitalize(a,LEN+1);
    for(i=0;i<(LEN+1);i++)
    {
        printf("%c",a[i]);
    }                                                                                            
    
    
}

void capitalize(char a[], int N+1)
{
    int i;
    
    for(i=0;i<(LEN+1);i++){
        a[i]=toupper(a[i]);
        
    }
}

报错了 哪不对呢

修改了下还是报错

#include<stdio.h>
#include<ctype.h>

#define  LEN  100

void capitalize(char *,int );

int main()
{
    int i;
    char a[LEN+1] = "Asawdw1123,.";
    capitalize(a,LEN+1);
    for(i=0;i<(LEN+1);i++)
    {
        printf("%s",a);
    }                                                                                            
    
    
}

void capitalize(char *, int )
{
    int i;
    char ch;
    for(i=0;i<(LEN+1);i++){
        a[i]=toupper(a[i]);
       
        
    }
}

 再次更改 可以了 是函数类型错了

#include<stdio.h>
#include<ctype.h>

#define  LEN  100

void capitalize(char a[],int );

int main()
{
    int i;
    char a[LEN+1] = "Asawdw1123,.";
    capitalize(a,LEN+1);
    for(i=0;i<(LEN+1);i++)
    {
        printf("%c",a[i]);
    }                                                                                            
    
    
}
void capitalize(char a[] , int )
{
    int i;
    char ch;
    for(i=0;i<(LEN+1);i++){
        a[i]=toupper(a[i]);
        
        
    }
}

 (b)重写capitalize函数,这次使用指针。

#include<stdio.h>
#include<ctype.h>

#define  LEN  100

void capitalize(char *,int );

int main()
{
    
    char a[LEN+1] = "Asawdw1123,.";
    
    capitalize(a,LEN+1);

    printf("%s",a);
                                                                                                
}
void capitalize(char * , int  )
{
    
    while(*s++ != '\0')
    {
        *s = toupper(*s);
    }
}

 

 

 报错了。
更改调用函数类型 好了

#include<stdio.h>
#include<ctype.h>

#define  LEN  100

void capitalize(char *,int );

int main()
{
    
    char a[LEN+1] = "Asawdw1123,.";
    
    capitalize(a,LEN+1);

    printf("%s",a);
                                                                                                
}
void capitalize(char *s , int  )
{
    
    while(*s++ != '\0')
    {
        *s = toupper(*s);
    }
}

总结:字符串 

'\0'

指针和函数调用温习下

posted on 2022-02-04 11:22  米缸虫  阅读(23)  评论(0编辑  收藏  举报