c語言在字元串中查找
1. 一個簡單c語言題目,大神求助!在字元串中查找指定字元
1、打開python語言命令窗口,定義字元串變數s1並進行賦值。
2. C語言文件中字元串的查找與替換
用"rb" open, 用 fread 讀, 用 fwrite 寫.
記錄地點 用 fgetpos.
下面程序例子是按你的原來題意,找目標字串,輸出用替代字.直接用文件操作,不開單元存放文件.
輸入文件 a.txt 輸出文件 tmp.txt
至於你的新要求,給你提示記錄地點的方法.你可以rewind(fin),從頭一個字一個字讀,讀一個輸出一個,讀到的位置等於POS[i]-80時,讀80個字但不輸出,這就去掉了80個字.
例如找到的POS[]共NN個。
#define buff_size 1024
long int n,n1,n2,i,j,k;
char *buff;
buff=(char*) malloc(buff_size * sizeof(char));
rewind(fin);
for(k=0;k<NN;k++){
if (k==0) {n=POS[k]-80;} else {n=POS[k]-POS[k-1]-80;};
n1 = n / buff_size; n2 = n % buff_size;
if (n1 >0) for (i=0;i<n1;i++){
fread(buff,sizeof(char),buff_size,fin);
fwrite(buff,sizeof(char),buff_size,fout);
};
if (n2 > 0) {fread(buff,sizeof(char),n2,fin);
fwrite(buff,sizeof(char),n2,fout);
};
fread(buff,sizeof(char),80,fin);
for (i=0;i<80;i++) buff[i]='0'; buff[80]='\0';
fwrite(buff,sizeof(char),80,fout);
} ; end for k
這里請自己寫 讀最後一段,無需改零,讀一個字,輸出一個字,直到EOF.
-----------------
#include <stdio.h>
#include <stdlib.h>
void main (int argc, char *argv[])
{
FILE *fin,*fout;
char namein[72]="a.txt";
char nameout[72]="tmp.txt";
char target[120],tidai[120];
char work[120];
int L1,L2,i,k=0;
int numread;
fpos_t pos;
fpos_t POS[100];
printf("Enter target string: ");
scanf("%s",&target[0]);
L1 = strlen(target);
printf("Enter replace string: ");
scanf("%s",&tidai[0]);
L2 = strlen(tidai);
if ( (fin = fopen(namein,"rb") ) == NULL ) {
printf("\007Cann't open input file: %s ", namein);exit(1);
};
if ( (fout = fopen(nameout,"wb") ) == NULL ) {
printf("\007Cann't open temp work file: %s ", nameout);exit(1);
};
Lab1:
numread = fread( work, sizeof( char ), L1, fin );
if (numread < L1) {
// fwrite( work, sizeof( char ), numread, fout );
goto done;
};
if ( strncmp(work,target,L1) == 0 ){
// fwrite( tidai, sizeof( char ), L2, fout );
if( fgetpos( fin, &pos ) != 0 ) perror( "fgetpos error" );
else { POS[k] = pos-L1; k=k+1;};
goto Lab1;
};
Lab2:
// fwrite( &work[0], sizeof( char ), 1, fout );
for (i=0;i<L1-1;i++) work[i]=work[i+1];
fread( &work[L1-1], sizeof( char ), 1, fin );
if (feof(fin)) {
// fwrite( &work[1], sizeof( char ), L1-1, fout );
goto done;
};
if ( strncmp(work,target,L1) == 0 ){
// fwrite( tidai, sizeof( char ), L2, fout );
if( fgetpos( fin, &pos ) != 0 ) perror( "fgetpos error" );
else { POS[k] = pos-L1; k=k+1;};
goto Lab1;
} else {goto Lab2;};
// 新述 rewind(fin); 那部分程序語句插在這里,聲明放前面
done: fclose(fin);fclose(fout);
printf("output in %s\n",nameout);
for (i=0;i<k;i++){
printf("%ld \n",(long int) POS[i]);
}
exit(0);
}
3. c語言如何查找字元串
C語言中的標准函數庫中的strchr()函數可以實現查找字元串中的某個字元。
C語言strchr()函數:
查找某字元在字元串中首次出現的位置
頭文件:#include <string.h>
strchr() 用來查找某字元在字元串中首次出現的位置,其原型為:
char * strchr (const char *str, int c);
【參數】str 為要查找的字元串,c 為要查找的字元。
strchr() 將鍵銀會找出 str 字元串中第一次出現的字元 c 的地址,然後將該地址返回。
注意:字元串 str 的結束標志 NUL 也會被納入檢索范圍,所以 str 的物亮滲組後一個字元也可以被定位。
【返回值】如果找到指定的字元則返回該字元所在地址,否則返回 NULL。
返回的地址是字元串在內存中隨機分配的地罩脊址再加上你所搜索的字元在字元串位置。設字元在字元串中首次出現的位置為 i,那麼返回的地址可以理解為 str + i。
提示:如果希望查找某字元在字元串中最後一次出現的位置,可以使用 strrchr() 函數。
4. C語言中在字元串中查找指定字元串
#include"stdio.h" #include"string.h" void main() { char *str1="nayitian anzd"; char *str2="anz"; int index[20]; int num = 0; int i,j; for (i = 0; i < strlen(str1) - 3; i++) for (j = 0; j < 3; j++) { if (*(str1 + i) == *(str2 + j)) //比較 { i ++; //str1移到下一位 if (j == 2) //如果比較了三次都相等則找到一個 { index[num] = i - 2; //記錄str1下標 num ++; } } else { i = i - j; //有不相等的跳出,i回到比較前的值 break; } } printf("共找到:%d個\n",num); printf("下標分別是:"); for (i = 0; i < num; i++) { printf("\t%d",index[i]); } getchar(); }
5. C語言實現在一個字元串中查找指定的字元,並輸出指定字元在字元串中出現的次數和位置
package com.string.to;
import java.util.Arrays;
import java.util.Scanner;
public class JudeCount{
public static void main(String[]args){
System.out.println("請輸入你要判斷的字元串:");
Scanner s=new Scanner(System.in);
String str=s.nextLine();
char[]ch=str.toCharArray();
Arrays.sort(ch);//對數組排序
char max='a';//記錄出現次數最多元素
int maxcount=0;//記錄最大出現次數
int count=1;//中間傳值參數判斷當前元素出現次數
for(int i=0;i<ch.length-1;i++){//進行判斷
if(ch<i>==ch[i+1]){
count++;
}
if(ch<i>!=ch[i+1]){
if(count>maxcount){
maxcount=count;
max=ch<i>;
}
count=1;
}
}
System.out.println("出現最多的元素是:"+max+"次數為:"+maxcount);
}
}
(5)c語言在字元串中查找擴展閱讀:
system函數用法:
用法:intsystem(char*command);
程序例:
#include<stdlib.h>
#include<stdio.h>
intmain(void)
{
printf("AbouttospawnandrunaDOScommand\n");
system("dir");
return0;
}
又如:system("pause")可以實現凍結屏幕,便於觀察程序的執行結果;system("CLS")可以實現清屏操作。而調用color函數可以改變控制台的前景色和背景,具體參數在下面說明。
例如,用system("color0A");其中color後面的0是背景色代號,A是前鬧扒握景色代號。各顏色代碼如此純下:
0=黑色1=藍色2=綠色3=湖藍色4=紅色5=紫色6=黃色7=白色8=灰色9=淡藍色A=淡綠色B=淡淺綠色C=淡紅色D=淡紫色E=淡黃色F=亮白色
(注意:MicrosoftVisualC++6.0支持system)
顏色屬性由兩個十六進制數字指定--第一個對應於背景,第二個對應於前景。每個數字
可以為以下任何值:
0=黑色8=灰色
1=藍色9=淡藍色
2=綠色A=淡綠色
3=淺綠色B=淡淺綠色
4=紅色C=淡紅色
5=紫色D=淡紫色
6=黃色E=淡黃色
7=白液慶色F=亮白色