美文网首页
2019-12-01

2019-12-01

作者: Jiawei_84a5 | 来源:发表于2019-12-01 17:51 被阅读0次
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// argc: 统计运行程序时命令行参数的个数
// *argv[]: 储存每个参数的字符串指针,每一个元素对应一个参数
int main (int argc,char *argv[])
{
    int ch,bian=0,count=0;
    char find[50],temp[1000];
    FILE *pt;    //文件指针
    // 判断是否输入文件
    if (argc != 2){
        printf("请使用格式: %s 文件名",argv[0]); 
        exit(1);    //非正常退出
    }
    // 判断能否成功打开文件
    if ((pt = fopen(argv[1],"r"))== NULL){  //将argv[1]赋值给指针pt
        printf("打开文件 %s 失败",argv[1]);
        exit(1);   
    }
    printf("请输入要查找的单词:");
    gets(find);
    while((ch=getc(pt))!=EOF){  //EOF 表示文件结束
        if ((ch!=' ')&&(ch!='\n')){
            temp[bian]=ch;
            bian++;
        } else {
            temp[bian]='\0';   
            if ((strcmp(find,temp))==0) count++;
            bian=0;
        }
    }
    printf("共在文件%s中查找到字符串\"%s\" %d 个\n",argv[1],find,count);
    getch();
    return 0;
}
#include <stdio.h>
#include <string.h>
#define STR_LEN 30
char *myStrcpy(char *strDest, const char *strSrc){
    if(strDest == NULL || strSrc == NULL)
        return NULL;
    
    if(strDest == strSrc)
        return strDest;
    char *tempptr = strDest ;
    while( (*strDest++ = *strSrc++) != '\0');
        return tempptr ;
}
int main( void ){
    char *strSrc = "http://www.coderbbs.com";
    char strDest[STR_LEN];
    memset(strDest,0 , STR_LEN);  // 将strDest指向的内存全部设置为 NUL
    
    printf("strDest = %s\n", strDest);
    myStrcpy(strDest, strSrc);
    printf("strDest = %s\n", strDest);
    return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
// argc: 统计运行程序时命令行参数的个数
// *argv[]: 储存每个参数的字符串指针,每一个元素对应一个参数
int main (int argc,char *argv[])
{
    int ch,bian=0,count=0;
    char find[50],temp[1000];
    FILE *pt;    //文件指针
    // 判断是否输入文件
    if (argc != 2){
        printf("请使用格式: %s 文件名",argv[0]); 
        exit(1);    //非正常退出
    }
    // 判断能否成功打开文件
    if ((pt = fopen(argv[1],"r"))== NULL){  //将argv[1]赋值给指针pt
        printf("打开文件 %s 失败",argv[1]);
        exit(1);   
    }
    printf("请输入要查找的单词:");
    gets(find);
    while((ch=getc(pt))!=EOF){  //EOF 表示文件结束
        if ((ch!=' ')&&(ch!='\n')){
            temp[bian]=ch;
            bian++;
        } else {
            temp[bian]='\0';   
            if ((strcmp(find,temp))==0) count++;
            bian=0;
        }
    }
    printf("共在文件%s中查找到字符串\"%s\" %d 个\n",argv[1],find,count);
    getch();
    return 0;
}

相关文章

网友评论

      本文标题:2019-12-01

      本文链接:https://www.haomeiwen.com/subject/emttgctx.html