c語言查找字符串中某個字符,c語言如何查找文件中的某個字符串

本文目錄一覽:

c語言編寫一個程序,實現查找一個字符串中的特定字符,並將其刪除.

一、算法描述

逐個比較字符串source中的字符,若當前i位置的字符等於待刪除字符ch,則i+1..len-1之間的子串整體前移;如此反覆,直到所有待刪除字符都找到並被刪除為止。

二、操作過程

三、參考程序

#include stdio.h

#include string.h

/* 移除字符串source中的所有ch字符 */

void remove(char *source, char ch);

void main()

{

char source[1000];

char ch;

printf(“請輸入一個字符串:”);

gets(source);

printf(“請輸入待刪除字符:”);

ch = getchar();

remove(source, ch);

printf(“新的字符串:”);

puts(source);

}

/* 移除字符串source中的所有ch字符 */

void remove(char *source, char ch)

{

int i, j;

int len = strlen(source);

for(i=0; source[i]!=’\0′; i++)

{

if(source[i] == ch)

{

for(j=i+1; source[j]!=’\0′; j++)

{

source[j-1] = source[j];

}

source[j-1] = ‘\0’;

}

}

}

四、運行測試

請輸入一個字符串:How are you?

請輸入待刪除字符:o

新的字符串:Hw are yu?

C語言如何取一串字符串中的某個字符

C中的字符串就是一個字符數組。

如:

char s[10]=”wo shi SB”;

char c;

取最左邊的字符,就是c=s[0];

編寫程序實現在一個字符串中查找指定的字符(請用c語言作答)

#includelt;stdio.hgt;

int main()

{

int i,index,count;

char a,ch,str[80];

scanf(“%c\n”,a);

i=0;

index=-1;

count=0;

ch=getchar();

for(i=0;ch!=’\n’;i++){

stri=ch;

count++;

ch=getchar();

}

for(i=0;ilt;count;i++)

if(a==stri)

index=i;

if(index!=-1)

printf(“index=%d”,index);

else

printf(“Not Found”);

return 0;

}

擴展資料:

getchar()用法:

getchar()函數的作用是從計算機終端(一般為鍵盤)輸入一個字符。getchar()函數只能接收一個字符,其函數值就是從輸入設備得到的字符。

例:

#includelt;stdio.hgt;

int main(void)

{

int c;

/*Note that getchar reads from stdin and

is line buffered;this means it will

not return until you press ENTER.*/

while((c=getchar())!=’\n’)

printf(“%c”,c);

return 0;

}

註:可以利用getchar()函數讓程序調試運行結束後等待編程者按下鍵盤才返回編輯界面,用法:在主函數結尾,return 0;之前加上getchar();

C語言中怎麼查找字符串數組中的某個字符?

錯誤在於你判斷了第一個非@字符時就已經輸出沒有字符@退出循環了所以不會檢測@了。改成下面就行了:#include stdio.h

#include string.h

int main()

{

char sh[100],n=0;

gets(sh);

for(int i=0;sh[i];i++)

if(sh[i]==’@’)

n++;

if(n==0)

printf(“沒有字符 @\n”);

else

printf(“有字符 @\n”);

}

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;ilt;ch.length-1;i++){//進行判斷

if(chi==ch[i+1]){

count++;

}

if(chi!=ch[i+1]){

if(countgt;maxcount){

maxcount=count;

max=chi;

}

count=1;

}

}

System.out.println(“出現最多的元素是:”+max+”次數為:”+maxcount);

}

}

擴展資料:

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=亮白色

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/198994.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-04 10:51
下一篇 2024-12-04 19:13

相關推薦

發表回復

登錄後才能評論