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