c語言函數(shù)最長單詞 c語言最長平臺

C語言輸出一行字符串中最長的單詞

代碼如下:

創(chuàng)新互聯(lián)建站堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的澧縣網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

#包括 stdio, h

# include string。H

Voidmain()

字符c[50];

Voidf(charc[50]);//沒有返回值,直接用函數(shù)f打印結(jié)果。

(c);

F(c);

Voidf(charc[50])

Intlen=0,lenth=0,I,flag=0;

For(I=0;C(我)?。剑В?';我++)

如果(c[我]?。剑ВВ?/p>

Len++;

If(c[I]=='')

If (len lenth)

長度=蘭;

國旗=i-lenth;//當有一個新的最長的字符串時,使用flag記錄字符串開始下標

Len=0;

If (len lenth)

長度=蘭;

國旗=i-lenth;//以同樣的方式

Printf("最長的子串是%d字符,子串是:",lenth);//打印子串的長度

For (I = flag;I flag + lenth;i++)//將子字符串從flag輸出到flag + lenth-1

Printf("%c",[I]c);

Printf("\n");//換行

擴展資料:

1.返回意味著從被調(diào)用的函數(shù)繼續(xù)執(zhí)行到主函數(shù)。Return可以伴隨一個返回值,返回值可以是常量、變量或表達式。

2.操作:結(jié)束正在運行的函數(shù)并返回該函數(shù)的值。

3.返回值:

返回值可以是各種數(shù)據(jù)類型,例如:int、float、ouble、char、a[](數(shù)組)、*a(指針)、

結(jié)構(gòu)或類(c++)返回類型指定返回后要添加的金額的類型,如果返回類型被聲明為void,則不需要返回值。PublicstaticvoidMain()//programentry只能被聲明為void和int的返回

{

//沒有返回值

}

公共靜態(tài)空隙(mm)

{

返回;//在某些情況下,在一個voidmethod中,需要跳出來,你可以直接使用return,不能在后面添加任何數(shù)量

}

公共靜態(tài)intcc()

{

321的回歸;因為返回類型聲明為int,所以返回一個整數(shù)

}

公共靜態(tài)字符串MSMSM()

{

返回“asdfhasjghdg”;因為返回類型被聲明為string,所以返回一個string

}

C語言用函數(shù)求字符串中最長單詞.

#includestdio.h

#includestring.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=0;str[i];i++)

{

if((str[i]='a'str[i]='z')||(str[i]='A'str[i]='Z'))

{

if(currLen==0)//當前長度等于0時,說明一個單詞剛剛查找完,此時的位置為

currStart=i;//新單詞的開始位置

currLen++;

}

if(currLenmaxLen)//當前單詞的長度大于最大值,進行賦值

{

maxLen = currLen;

MaxStart = currStart;

}

if(str[i]==' ')//遇到空格,即一個單詞已經(jīng)結(jié)束。

{

currLen = 0;

}

}

for(j=MaxStart;jMaxStart+maxLen;j++)

printf("%c",str[j]);

}

運行效果:

擴展資料:

Return用法

1.含義:return表示從被調(diào)函數(shù)返回到主調(diào)函數(shù)繼續(xù)執(zhí)行,返回時可附帶一個返回值,返回值可以是一個常量,變量,或是表達式。

2.作用:結(jié)束正在運行的函數(shù),并返回函數(shù)值。

3.返回值:

計算結(jié)果表示函數(shù)執(zhí)行的順利與否(-1、0)返回值可以為各種數(shù)據(jù)類型,如:int,float,ouble,char,a[](數(shù)組),*a(指針),

結(jié)構(gòu)或類(c++)返回類型規(guī)定了return后面所加的量的類型,如果返回類型聲明為void,則不需要返回值。public static void Main()//程序入口只可以聲明為void和int的返回

{

//不需要返回值就OK

}

public static void mm()

{

return;//有些時候,在void的方法中,你需要跳出它,可以直接用return而不能加任何量在后面

}

public static int cc()

{

return 321;//因為返回類型聲明為int,所以返回一個整數(shù)

}

public static string msmsm()

{

return“asdfhasjghdg”;//因為返回類型聲明為string,所以返回一個字符串

}

c語言函數(shù)的最長單詞輸出問題 新手求助大佬

void f(char a[])

{

int pos,tpos,len=0,tlen=0;

int i=0;

do

{

if ((a[i]='a' a[i]='z')||(a[i]='A' a[i]='Z'))

{

if (!tlen) tpos=i;

tlen++;

}

else

{

if (tlenlen)

{

pos=tpos;

len=tlen;

}

tlen=0;

}

}

while (a[i++]);

for (i=0;ilen;i++) printf("%c",a[pos+i]);

}

C語言求最長單詞

#includecstdio

#includecstring

using?namespace?std;

const?int?maxn=100; //這個表示單詞可能的最大個數(shù)

const?int?maxl=100; //這個表示單詞可能的最長長度,需要根據(jù)題目要求確定

int?n,maxlen;

int?len[maxn];

char?ch[maxn][maxl];

int?max(int?a,int?b){

return?ab?a:b;

}

int?main(){

int?k=0;

while(~scanf("%c",ch[n][k])){ //每次輸入一個字符,n表示當前是第幾個單詞,k表示是這個單詞的第幾個字母

if(ch[n][k]!='?'??ch[n][k]!='\n'){ //不是空格或空行說明還沒有讀完,k++,接著讀下一個

k++;continue;

}

len[n]=k-1; //計算這個單詞的長度

maxlen=max(maxlen,len[n]); //更新最長的單詞長度

if(ch[n][k]=='?')?k=0,n++; //如果讀到空格,說明這個單詞讀完了

else{ //如果讀到空行,說明這組數(shù)據(jù)讀完了,開始輸出這組數(shù)據(jù)的答案

for(int?i=0;i=n;i++)

if(len[i]==maxlen) //如果長度等于最長的單詞長度,這個單詞就是最長單詞

printf("%s",ch[i]); //輸出即可

putchar('\n');

memset(ch,0,sizeof(ch)); //將原來的數(shù)組清空

n=0;maxlen=0;k=0;

}

}

return?0;

}

我自己測了一組輸入數(shù)據(jù),答案應(yīng)該沒什么問題了:

Hello?sir?are?you?satisfied?with?my?answer??

Yes?I?feel?thankful?of?your?answer?.

How?does?it?feel??

It?feels?very?good?!

輸出:

satisfied??

thankful??

does?feel??

feels

等等,我才發(fā)現(xiàn)是按字典序輸出...我這是按輸入順序輸出的....

#includecstdio

#includecstring

#includealgorithm

using?namespace?std;

const?int?maxn=100; //這個表示單詞可能的最大個數(shù)

const?int?maxl=100; //這個表示單詞可能的最長長度,需要根據(jù)題目要求確定

int?n,maxlen;

struct?Word{

int?len;

char?ch[maxl];

void?clean(){

memset(ch,0,sizeof(ch));

}

}word[maxn];

int?max(int?a,int?b){

return?ab?a:b;

}

bool?cmp(const?Word?a,const?Word?b){//比較兩個單詞的函數(shù)

return?strcmp(a.ch,b.ch)0;

}

int?main(){

int?k=0;

while(~scanf("%c",word[n].ch[k])){ //每次輸入一個字符,n表示當前是第幾個單詞,k表示是這個單詞的第幾個字母

if(word[n].ch[k]!='?'??word[n].ch[k]!='\n'){ //不是空格或空行說明還沒有讀完,k++,接著讀下一個

k++;continue;

}

word[n].len=k-1; //計算這個單詞的長度

maxlen=max(maxlen,word[n].len); //更新最長的單詞長度

if(word[n].ch[k]=='?')?k=0,n++; //如果讀到空格,說明這個單詞讀完了

else{ //如果讀到空行,說明這組數(shù)據(jù)讀完了,開始輸出這組數(shù)據(jù)的答案

sort(word,word+n+1,cmp); //將所有單詞按照字典序排序

for(int?i=0;i=n;i++)

if(word[i].len==maxlen) //如果長度等于最長的單詞長度,這個單詞就是最長單詞

printf("%s",word[i].ch); //輸出即可

putchar('\n');

for(int?i=0;i=n;i++)

word[i].clean(); //將原來的數(shù)組清空

n=0;maxlen=0;k=0;

}

}

return?0;

}

上面這個是修改稿。

測試數(shù)據(jù):

Hello?sir?are?you?satisfied?with?my?answer??

Yes?I?feel?thankful?of?your?answer?.

How?does?it?feel??

It?feels?very?good?!

What?feel?it?does??

輸出結(jié)果:

satisfied??

thankful??

does?feel??

feels??

What?does?feel

最后一個What先輸出不是錯誤哦...

因為W是大寫...所以字典序比其他的小

新聞標題:c語言函數(shù)最長單詞 c語言最長平臺
網(wǎng)頁URL:http://muchs.cn/article14/dohdpge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、電子商務(wù)網(wǎng)站設(shè)計、微信公眾號小程序開發(fā)、微信小程序

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)