大寫(xiě)轉(zhuǎn)小寫(xiě)c語(yǔ)言 C語(yǔ)言用函數(shù)求字符串中最長(zhǎng)單詞?
C語(yǔ)言用函數(shù)求字符串中最長(zhǎng)單詞?#include<stdio.h>#include<string.h>int main(){char str[100]gets(str)void fin
C語(yǔ)言用函數(shù)求字符串中最長(zhǎng)單詞?
#include<stdio.h>
#include<string.h>
int main()
{
char str[100
]gets(str)
void findLongest(char str[])
findLongest(str)
return 0
}
void findLongest(char str[])
{
int currLen=0,maxLen=0,currStart=0,MaxStart=0
int i=0,j=0
for(i=0str i)
{((STR[i]>=“a”&&;ampstr[i]<=“Z”);(STR[i]>=“a”&;ampstr[i]<=“Z”)
{
if(currlen==0)//當(dāng)前長(zhǎng)度等于0時(shí),表示剛搜索了一個(gè)單詞,位置為
currstart=I//新詞的起始位置
currlen
}
]如果(currlen>maxlen)//當(dāng)前詞的長(zhǎng)度大于最大值,則賦值
{
maxlen=currlen
maxstart=currstart
}
如果(STR[I]=”)//遇到空格,即一個(gè)詞有結(jié)束了。
要實(shí)現(xiàn)此功能,我們需要按照以下步驟編程:1。確定詞的分隔符,一般是空格和標(biāo)點(diǎn)符號(hào),并根據(jù)標(biāo)題的情況確定標(biāo)點(diǎn)符號(hào)是否包含在詞中。假設(shè)這個(gè)詞不包括標(biāo)點(diǎn)符號(hào),也就是說(shuō),它是由所有字母組成的。2根據(jù)以上特點(diǎn),讀一個(gè)字3,計(jì)算字長(zhǎng)4,與當(dāng)前最大字長(zhǎng)max(初始值為0)比較,如果大于max,則記錄當(dāng)前字,將其長(zhǎng)度賦給Max5,重復(fù)2-4,直到讀完字(到字符串末尾)。6輸出最長(zhǎng)單詞長(zhǎng)度和單詞7。常規(guī):包括。H>#包含<string。H>#define m 1000 int main(){int low//單詞int high的起始下標(biāo)//單詞int i的結(jié)束位置//循環(huán)變量int count//計(jì)算最長(zhǎng)單詞int temp的長(zhǎng)度//中間變量int low temp int high temp char P[m]//具有多個(gè)單詞的字符指針get(P)count=0 low=0高=0為(I=0 I< strlen(P)I){temp=0低 emp=I而(P[I]!=“”&;P[i]!=“ ”)//P[i]!=space{temp I}highutemp=I-1 if(temp> count){count=temp low=lowutemp high=highutemp}}for(I=low I<=high I){putchar(p[I])}return 0}
include<stdio.h>char*longest(char*p){char t,max,n while(*p=“”)p對(duì)于(max=n=0,t=p*pp)如果(*p!=“”)n else{if(max<=n)max=n,t=p-n n=0}返回max<=n?p-n:t}int main(void){char str[1000],*p scanf(“%[A-Z A-Z]”,str)for(p=最長(zhǎng)(str)*p&*p!=“”printf(”%c“,*p))printf(“n”)返回0}