
c语言编程,请高手帮忙!有最高加分!!!200
用C语言编写软件完成以下任务:输入一行字符存入文件myfile.txt中,读取文件数据后统计字符的个数,统计喊有某一字串的个数。例如:在abxyefghabcd中有ab子...
用C语言编写软件完成以下任务:输入一行字符存入文件myfile.txt中,读取文件数据后统计字符的个数,统计喊有某一字串的个数。例如:在abxyefghabcd中有ab子串2个。
展开
2008-03-12
展开全部
我做到了,希望楼主也加到满分哟,呵呵。
程序在找子串时,如在abcccc中找子串cc时结果是3个而不是2个,当然楼主如果要求是2个稍改下就成。
输入字符串时允许空格。
用法:
1 输入一串字符,如123 abvc uuuu(回车)
2 程序显示统计有多少字符的结果,并要求输入想查的子串此时输入想查的子串,如 uu(回车)
3 程序显示有3个uu子串在里边。
注:我规定的是只有字母才算字符。
myfile.txt生成在D盘上。
编译环境TC2.0
#include"stdio.h"
#include"stdlib.h"
void main()
{
FILE *fp;
char ch,*p,*pcopy,*pp,a[1000],*tiaojian;
int i=0;
if((fp=fopen("d:\\myfile.txt","w+"))==NULL)
{
printf("Cannot open file strike any key exit!");
getch();
exit(1);
}
printf("please input the sentence here : \n");
gets(p);
while(*p!='\0') {
ch=*p++;
fputc(ch,fp);} /*写到myfile.txt*/
fclose(fp);
if((fp=fopen("d:\\myfile.txt","r+"))==NULL) /*从myfile.txt中读取文件*/
{
printf("Cannot open file strike any key exit!");
getch();
exit(1);
}
p=a;
while ((*p++=fgetc(fp))!=EOF) {
if ((*(p-1)<='z' && *(p-1)>='a') || (*(p-1)<='Z' && *(p-1)>='A')) i++;}
*p='\0';
printf("\n\nCOUNT:there are %d words in your sentence!",i);
p=a;
printf("\n\ninput the words that you want to look for: \n");
fflush(stdin);
gets(tiaojian);
pp=tiaojian;
i=0;
while (*p!='\0') {
pcopy=p++;
while (*pcopy++==*tiaojian++) {if (*tiaojian=='\0') {i++;break;}}
tiaojian=pp; }
printf("\n\n%d ge zi chuan!",i);
fclose(fp);
getch();
}
程序在找子串时,如在abcccc中找子串cc时结果是3个而不是2个,当然楼主如果要求是2个稍改下就成。
输入字符串时允许空格。
用法:
1 输入一串字符,如123 abvc uuuu(回车)
2 程序显示统计有多少字符的结果,并要求输入想查的子串此时输入想查的子串,如 uu(回车)
3 程序显示有3个uu子串在里边。
注:我规定的是只有字母才算字符。
myfile.txt生成在D盘上。
编译环境TC2.0
#include"stdio.h"
#include"stdlib.h"
void main()
{
FILE *fp;
char ch,*p,*pcopy,*pp,a[1000],*tiaojian;
int i=0;
if((fp=fopen("d:\\myfile.txt","w+"))==NULL)
{
printf("Cannot open file strike any key exit!");
getch();
exit(1);
}
printf("please input the sentence here : \n");
gets(p);
while(*p!='\0') {
ch=*p++;
fputc(ch,fp);} /*写到myfile.txt*/
fclose(fp);
if((fp=fopen("d:\\myfile.txt","r+"))==NULL) /*从myfile.txt中读取文件*/
{
printf("Cannot open file strike any key exit!");
getch();
exit(1);
}
p=a;
while ((*p++=fgetc(fp))!=EOF) {
if ((*(p-1)<='z' && *(p-1)>='a') || (*(p-1)<='Z' && *(p-1)>='A')) i++;}
*p='\0';
printf("\n\nCOUNT:there are %d words in your sentence!",i);
p=a;
printf("\n\ninput the words that you want to look for: \n");
fflush(stdin);
gets(tiaojian);
pp=tiaojian;
i=0;
while (*p!='\0') {
pcopy=p++;
while (*pcopy++==*tiaojian++) {if (*tiaojian=='\0') {i++;break;}}
tiaojian=pp; }
printf("\n\n%d ge zi chuan!",i);
fclose(fp);
getch();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int GetSubstrNum(char* S, char* T);
int main(int argc, char* argv[])
{
char T[81];
FILE *infp, *outfp;
if(!(infp = fopen("myfile.txt", "rt"))){
printf("error on open myfile.txt file!\n");
getchar();
exit(1);
}
if(!(outfp = fopen("Statistic.txt","wt"))){
printf("error on open Statistic.txt file!\n");
getchar();
exit(1);
}
printf("请输入要寻找的子串:");
scanf("%s",T);
int count = 0;
while(!feof(infp)){
char str[81];
if(fscanf(infp, "%s", str) == EOF)
break;
for(int index = 0; str[index] >='a' && str[index] <= 'z'; index++){
count++;
}
fprintf(outfp,"%s", str);
fprintf(outfp, " 字符串个数 %d",count);
fprintf(outfp, " 相同子串%s数 %d\n", T, GetSubstrNum(str, T));
}
fclose(outfp);
fclose(infp);
return 0;
}
int GetSubstrNum(char* S, char* T)
{
if(strlen(S)== 0 || strlen(T) == 0)
return 0;
int i = 0, j = 0;
int count = 0;
while(i < strlen(S)){
if(S[i] == T[j]){
++i;
++j;
}
else{
i = i-j+1;
j = 0;
}
if(j >= strlen(T)) {
count++;
j = 0;
}
}
return count;
}
示例:
asdafasldfasldkfjalskdjflasdjflksdfjlasdjflas 字符串个数 45 相同子串sdf数 1
#include <stdlib.h>
#include <string.h>
int GetSubstrNum(char* S, char* T);
int main(int argc, char* argv[])
{
char T[81];
FILE *infp, *outfp;
if(!(infp = fopen("myfile.txt", "rt"))){
printf("error on open myfile.txt file!\n");
getchar();
exit(1);
}
if(!(outfp = fopen("Statistic.txt","wt"))){
printf("error on open Statistic.txt file!\n");
getchar();
exit(1);
}
printf("请输入要寻找的子串:");
scanf("%s",T);
int count = 0;
while(!feof(infp)){
char str[81];
if(fscanf(infp, "%s", str) == EOF)
break;
for(int index = 0; str[index] >='a' && str[index] <= 'z'; index++){
count++;
}
fprintf(outfp,"%s", str);
fprintf(outfp, " 字符串个数 %d",count);
fprintf(outfp, " 相同子串%s数 %d\n", T, GetSubstrNum(str, T));
}
fclose(outfp);
fclose(infp);
return 0;
}
int GetSubstrNum(char* S, char* T)
{
if(strlen(S)== 0 || strlen(T) == 0)
return 0;
int i = 0, j = 0;
int count = 0;
while(i < strlen(S)){
if(S[i] == T[j]){
++i;
++j;
}
else{
i = i-j+1;
j = 0;
}
if(j >= strlen(T)) {
count++;
j = 0;
}
}
return count;
}
示例:
asdafasldfasldkfjalskdjflasdjflksdfjlasdjflas 字符串个数 45 相同子串sdf数 1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
#include<stdlib.h>
void main()
{
char one_line[200];
char str[80],sub_str[10];
int i,j,gb,len,l,n;
int total_byte;
FILE *fout,*fin;
// write 3 line letters
fout = fopen("myfile.txt","w");
for (i=0;i<3;i++){
printf("Enter one line letters\n");
gets(&one_line[0]);
fprintf(fout,"%s\n",one_line);
};
fclose(fout);
// open file
fin= fopen("myfile.txt","r");
// how many chars
while ( gb = fgetc(fin) !=EOF ) { total_byte = total_byte + 1;};
rewind(fin);
printf("total_byte: %d\n",total_byte);
// how many sub_str
printf("Enter sub string ab\n");
scanf("%s",&sub_str[0]);
l=strlen(sub_str);
n=0;
while (fscanf(fin,"%s",&str[0]) !=EOF){
len = strlen(str);
if (l <= len){
for (i=0;i<=len-1;i++){
if ( strncmp(&str[0]+i,sub_str,l) == 0) n = n + 1;
}
}
}
fclose(fin);
printf("n=%d\n",n);
getch();
}
#include<stdlib.h>
void main()
{
char one_line[200];
char str[80],sub_str[10];
int i,j,gb,len,l,n;
int total_byte;
FILE *fout,*fin;
// write 3 line letters
fout = fopen("myfile.txt","w");
for (i=0;i<3;i++){
printf("Enter one line letters\n");
gets(&one_line[0]);
fprintf(fout,"%s\n",one_line);
};
fclose(fout);
// open file
fin= fopen("myfile.txt","r");
// how many chars
while ( gb = fgetc(fin) !=EOF ) { total_byte = total_byte + 1;};
rewind(fin);
printf("total_byte: %d\n",total_byte);
// how many sub_str
printf("Enter sub string ab\n");
scanf("%s",&sub_str[0]);
l=strlen(sub_str);
n=0;
while (fscanf(fin,"%s",&str[0]) !=EOF){
len = strlen(str);
if (l <= len){
for (i=0;i<=len-1;i++){
if ( strncmp(&str[0]+i,sub_str,l) == 0) n = n + 1;
}
}
}
fclose(fin);
printf("n=%d\n",n);
getch();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
#include<string.h>
void main()
{
FILE *fp;
char ch1[100],ch2[100];
int i,j,k,a,b,count=0;
printf("input the string:");
scanf("%s",ch1);
if((fp=fopen("F:\\myfile.txt","w"))==NULL)
{
printf("Can not open the file!\n");
exit(0);
}
j=strlen(ch1);
fwrite(ch1,j,1,fp);
fclose(fp);
printf("input the baby string:");
scanf("%s",ch2);
k=strlen(ch2);
if(k>j)
{
printf("input again:");
scanf("%s",ch2);
}
for(i=0;i<j-k+1;i++)
{
a=i;
b=0;
while((ch1[a]==ch2[b]) && a<=k+i-1)
{
a++;
b++;
}
if(a-1==k+i-1)
count++;
}
printf("%d\n",count);
}
#include<string.h>
void main()
{
FILE *fp;
char ch1[100],ch2[100];
int i,j,k,a,b,count=0;
printf("input the string:");
scanf("%s",ch1);
if((fp=fopen("F:\\myfile.txt","w"))==NULL)
{
printf("Can not open the file!\n");
exit(0);
}
j=strlen(ch1);
fwrite(ch1,j,1,fp);
fclose(fp);
printf("input the baby string:");
scanf("%s",ch2);
k=strlen(ch2);
if(k>j)
{
printf("input again:");
scanf("%s",ch2);
}
for(i=0;i<j-k+1;i++)
{
a=i;
b=0;
while((ch1[a]==ch2[b]) && a<=k+i-1)
{
a++;
b++;
}
if(a-1==k+i-1)
count++;
}
printf("%d\n",count);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用这个看嘛
#include<stdio.h>
#include<string.h>
void main()
{
FILE *fp;
char ch1[100],ch2[100];
int i,j,k,a,b,count=0;
printf("input the string:");
scanf("%s",ch1);
if((fp=fopen("F:\\myfile.txt","w"))==NULL)
{
printf("Can not open the file!\n");
exit(0);
}
j=strlen(ch1);
fwrite(ch1,j,1,fp);
fclose(fp);
printf("input the baby string:");
scanf("%s",ch2);
k=strlen(ch2);
if(k>j)
{
printf("input again:");
scanf("%s",ch2);
}
for(i=0;i<j-k+1;i++)
{
a=i;
b=0;
while((ch1[a]==ch2[b]) && a<=k+i-1)
{
a++;
b++;
}
if(a-1==k+i-1)
count++;
}
printf("%d\n",count);
}
#include<stdio.h>
#include<string.h>
void main()
{
FILE *fp;
char ch1[100],ch2[100];
int i,j,k,a,b,count=0;
printf("input the string:");
scanf("%s",ch1);
if((fp=fopen("F:\\myfile.txt","w"))==NULL)
{
printf("Can not open the file!\n");
exit(0);
}
j=strlen(ch1);
fwrite(ch1,j,1,fp);
fclose(fp);
printf("input the baby string:");
scanf("%s",ch2);
k=strlen(ch2);
if(k>j)
{
printf("input again:");
scanf("%s",ch2);
}
for(i=0;i<j-k+1;i++)
{
a=i;
b=0;
while((ch1[a]==ch2[b]) && a<=k+i-1)
{
a++;
b++;
}
if(a-1==k+i-1)
count++;
}
printf("%d\n",count);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询