C语言程序举例


C语言百例之读写字符串

字符串大小写转换

#include<stdio.h>
#include<ctype.h>
int main(int argc, char const *argv[])
{
    char ar;
    printf("please input some char 
");
    do
    {  // 大小写转换
        ar=getchar();
        if(islower(ar))//是大写
            ar=toupper(ar);//转换小写
        else
            ar=tolower(ar);
        putchar(ar);
    }while(ar!='q');
    return 0;
}

–对于这个例子,主要是字符大小写转换,利用的函数为toupper()和tolower();包含在函数ctype.h当中。

–在我们使用getchar()函数时,需要注意getchar()的原始形式中,输入先被缓冲,按入回车才返回写入到你需要的地方,这就是所谓的行缓冲输入。所以conio.h中提供了另外两个函数分别为 getche()和getch(); –

char br =getche();//代表写入直接回显;
getch()//不直接回显;

简单的打开关闭函数

#include<stdio.h>
#include<stdlib.h>
int main(int argc, char const *argv[])
{
    FILE*fp;
    char ch,filename[10];
    printf("Please input the name of the file :
" );
    scanf("%s",filename);
    if ((fp=fopen(filename,"r")==NULL))
    {
        printf("can't open the file 
");
        exit(0);
    }
    fclose(fp);
    return 0;
}

–本例简单的讲了文件的打开和关闭函数fopen();

原文地址:https://www.cnblogs.com/VCctor/p/5100706.html