当前位置:三九宝宝网 → 宝宝教育 → 教学论文 → 正文

Linux C语言在文件中查找字符串匹配关键字

更新:01-11 整理:39baobao.com
字体:

[用C语言程序通过自定义函数实现字符串处理函数strcat strcpy]0.0+【我自己做的 【strlen { char ch1[10]="abc"; int i; for(i=0;ch1[i]!='\0';i++);//循环到不是\0为假(结尾) printf("len=%d",i);//循环次数就是字符串的长度 getch(); } 【str...+阅读

#include

#include

#include

#define FILE_NAME_MAX 50

#define SEPERATE_STRING_MAX 100

int StrCount(FILE *file,char *str);

int main()

{

char *filename,*spestr;

FILE *fp;

filename=(char *)malloc(FILE_NAME_MAX);

spestr=(char *)malloc(SEPERATE_STRING_MAX);

printf("Input the filename:");

while(1)

{

scanf("%s",filename);

fp=fopen(filename,"r");

if(fp!=NULL)

{

break;

}

printf("Can't open the file.Try Again!");

}

printf("Input the special string:");

scanf("%s",spestr);

printf("%d times of %s in %s.",StrCount(fp,spestr),spestr,filename);

fclose(fp);

free(filename);

free(filename);

return 0;

}

int StrCount(FILE *file,char *str)

{

int count=0;

char ch;

int p=0;;

while((ch=fgetc(file))!=EOF)

{

// 当前读入的字符匹配 str 相应位置的字符

if(ch == str[p])

{

// 匹配下一个字符

p++;

// 如果已经匹配成功

if(str[p] == '\0')

{

count++;

// 从头开始重新匹配

p = 0;

}

}

// // 当前读入的字符不匹配 str 相应位置的字符

else

{

本文地址:https://www.39baobao.com/show/29_30045.html

以上内容来自互联网,请自行判断内容的正确性。若本站收录的信息无意侵犯了贵司版权,请联系我们,我们会及时处理和回复,谢谢.

以下为关联文档:

C语言文本字符串处理#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> int main(int argc, char **argv) { if (argc != 2) { printf("Usage: %s filename\n", st...

c语言编程:字符串处理#include #include #include int sortArr(int *arr, int count) { int i = 0, j = 0,tmp; for (i = 0; i { for (j = i + 1; j { if (arr[i]>arr[j]) { tmp = arr[i]; arr[i...

C语言的字符串处理的求指教A错误 str未赋初值 B在x16系统下正确,部分编译器正确 C++ x32下可以写成 1 2 3 4 5 DWORDflOldProtect; HANDLEhProc = GetCurrentProcess(); VirtualProtectEx(hProc, str4,...

c语言编程字符串处理#include<stdio.h> char str1[100],str2[100]; char *mystrcat(char *s,char *ct) //字符串连接 { while(*(s++)); s--; while(*ct) *(s++) = *(ct++); return s; } char *m...

C语言字符串的处理//最笨的方法,一个个判断 //当然查找子串有更好的算法,算法设计课上会学 char * mystrstr(char *s, char *t) { char *ps, *pt; for(; *s; ++s) { ps = s; pt = t; while(*ps+...

如何用C语言实现动态的字符串数组分成取数字与取非数字2个函数较简单。get_v()取数字, get_o()取非数字。 #include <stdio.h> char *get_v(char *a, char *b){ int i=0; while( a[i]>='0' & a[i]<='9') {b[i]=...

请问关于js语言有什么函数可以判断一个字符串中有几个中文字几个笨办法:一个字符串中,除去英文,数字,标点。剩下的即为中文。 function GetChineseCount(str){ var chinieseCount=0; var badChar ="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; badChar += "abc...

C语言中函数如何返回字符串别听 楼上 的 瞎说, 也 别 了, 都是 错 的, 我 告诉 你 正确 答案 函数 中 的 字符 串 在 函数 结束 的 时候 会 自动 被 释放 掉, 所以 即使 返回 char* 指向 的 也 就是 原 函...

关于字符串编码 C语言计算 abc的这个字符串的长度 Linuxstring A type that describes a specialization of the template class basic_string with elements of type char as a string. wstring A type that describes a special...