當前位置:編程學習大全網 - 編程軟體 - 編程在壹個已知字符串中查找最長單詞。假定字符串中只含有字母和空格,空格用來分

編程在壹個已知字符串中查找最長單詞。假定字符串中只含有字母和空格,空格用來分

// VC6.0 編譯運行通過

//沒用指針, 是數組, 應該沒問題吧

//字符串中的單詞可以用多個空格隔開。

# include <stdio.h>

# include <string.h>

# define LEN 1000

# define N 256

void f(char *s)

{

int i=0, j=0, k=0, t=0;

char str[N],ss[N];

for(i=0;s[i];i++)

{

if(' ' == s[i])

{

str[k] = '\0';

if(' ' == str[0])

{

for(t=0;str[t];t++)

{

str[t] = str[t+1];

}

str[t] = '\0';

}

if(strlen(str)>j)

{

strcpy(ss, str);

j = strlen(str);

}

strcpy(str,"");

k=0;

i++;

}

str[k] = s[i];

k++;

}

strcpy(s, ss);

}

int main(void)

{

char s[LEN] = " abcde abcdefghijklmnopqrstuvwxyz re mmtr stremd sgrmoremssss rms";

f(s);

printf("字符串中最長的單詞是:%s\n", s);

return 0;

}

  • 上一篇:用加減乘除實現位運算
  • 下一篇:機械三維造型設計軟件都有那些啊(機械三維圖設計用什麽軟件)
  • copyright 2024編程學習大全網