在C语言中,要求编写程序在给定的字符串中查找指定的字符。

在C语言中,要求编写程序在给定的字符串中查找指定的字符。,第1张

#include&ltstdio.h&gt

intmain()

{

inti,index,count

chara,ch,str[80]

scanf("%c\n",&a)

i=0

index=-1

count=0

ch=getchar()

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

str<i>=ch

count++

ch=getchar()

}

for(i=0i&ltcounti++)

if(a==str<i>)

index=i

if(index!=-1)

printf("index=%d",index)

else

printf("NotFound")

return0

}

扩展资料:

getchar()用法:

getchar()函数的作用是从计算机终端(一般为键盘)输入一个字岁清符。getchar()函数只能接收一个字符,其函数值就是从输入设备得到的字符。

例:

#include&ltstdio.h&gt

intmain(void)

{

intc

/*Notethatgetcharreadsfromstdinand

islinebufferedthismeansitwill

notreturnuntilyoupressENTER.*/

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

printf("%c",c)

return0

}

注:可以利用getchar()函数让程序调试运行结束后等待编程者乎猛前按下键盘才返回编辑界面,用法:在主函数结尾,return0;知圆之前加上getchar()

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=0i&ltch.length-1i++){//进行判断

if(ch<i>==ch[i+1]){

count++

}

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

if(count&gtmaxcount){

maxcount=count

max=ch<i>

}

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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12295327.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存