C语言 在字符串中找到最长单词

作者&投稿:燕怜 (若有异议请与网页底部的电邮联系)
c语言编程在一个一直的字符串中查找最长的单词,~

给出代码: #include #include int main(){ char s[128]; char *p1, *p2; int max=0, len=0; printf("Input a string: "); gets(s);//此处用get更好,get会将空格也输入 p1=s; for (int i=0; imax) { max=len; p2=p1; } len=0; } else // 如果当前字符非空,如果当前长度为0,则表示新单词。 { if (len==0) p1=&s[i]; len++; } } while (*p2 && *p2!=' ') printf("%c", *p2++);}

给出代码: #include #include int main(){ char s[128]; char *p1, *p2; int max=0, len=0; printf("Input a string: "); gets(s);//此处用get更好,get会将空格也输入 p1=s; for (int i=0; imax) { max=len; p2=p1; } len=0; } else // 如果当前字符非空,如果当前长度为0,则表示新单词。 { if (len==0) p1=&s[i]; len++; } } while (*p2 && *p2!=' ') printf("%c", *p2++);}

实现本功能,需要按以下步骤进行程序设计:
1、确定单词分隔符,一般情况为空格和标点符号,根据题目情况来确定标点符号是否算在单词中。 假定单词不包括标点符号,即全由字母组成
2、根据上面的特点,读取一个单词
3、计算单词长度
4、与当前最大单词长度max(初值为0)比较,如果大于max,则记录下当前单词,并将其长度赋值给max
5、重复2-4,直到读单词结束(到字符串尾)。
6、输出最长单词长度和单词

7、例程:

#include<stdio.h>
#include<string.h>
#define M 1000        
int main()
{
    int low;        // 单词的起始下标
    int high;        // 单词的结束位置
    int i;            // 循环变量 
    int count;        // 统计最长单词的长度
    int temp;        // 中间变量 
    int low_temp;
    int high_temp;
    char p[M];        // 存储有多个单词的字符指针
    gets(p);
    count = 0; 
    low = 0;
    high = 0;
    for(i = 0; i < strlen(p); i++)
    {
        temp = 0; 
        low_temp = i;
        while(p[i] != ' ' && p[i] != '\0')    // p[i] != 空格 
        {
             temp++;
             i++; 
        }
        high_temp = i-1;
        if(temp > count)
        {
            count = temp;
            low = low_temp;
            high = high_temp;
        }
    }
    for(i = low; i <= high; i++)
    {
        putchar(p[i]);
    }
    return 0;
}


我新写了一个,你看下吧, 主要用到了strtok()这个函数, 这个函数在做字符串切割时很有用的...

/*问题描述:
C语言 在字符串中找到最长单词
*/

#include <stdio.h>
#include <malloc.h>
#include <string.h>

#define MAX_STRING_LEN 100

void findTheMaxLen(char* string)
{
char* buf = NULL;
char* temp = NULL;
int preStringLen = 0;

for(temp = strtok(string, " "); temp; temp = strtok(NULL, " "))
{
preStringLen = strlen(temp);
buf = (char*)malloc((preStringLen + 1) * sizeof(char));
memset(buf, '\0', (preStringLen + 1) * sizeof(char));
if(strcmp(temp, buf) > 0)
strcpy(buf, temp);
}

printf("最长的字符串是: %s\n", buf);
free(buf);
buf = NULL;

}
void main()
{
char string[MAX_STRING_LEN] = {'\0'};

printf("请输入一个字符串: \n");
gets(string);

findTheMaxLen(string);

}

#include "stdio.h"
#include "string.h"

void main()
{
char str[100];
int maxLength=0,length=0,index,i;
printf("input:\n");
gets(str);
for(i=0;str[i];i++){
if(str[i]!=' ') {
length++;
if(length>maxLength){
index=i;
maxLength=length;
}
}else{
length=0;
}
}
printf("the longest string is:\n");
for(i=maxLength;i!=0;i--){
printf("%c",str[index-i+1]);
}
}

#include<stdio.h>
#include<conio.h>
#include<string.h>

void p(char ch[80])
{
int i=-1,j=0;
int start=0,mark=0;
int len=0,maxlen=0;
char out[80];
memset(out,sizeof(out),0x00);
do
{
i++;
if(ch[i]!=' '&&ch[i]!=0)
{
len++;
}
else
{
maxlen=len;
mark=i;
len=0;
}

}while(ch[i]!=0);

start=mark-maxlen;
for(i=0;i<maxlen;i++)
{
out[i]=ch[i+start];
}
out[i+1]=0;
printf("%s",out);
}

int main()
{
char c[80];
gets(c);
p(c);
getch();
return 0;
}

何必那么复杂,一边扫描输入字符串,一边计算当前单词的长度,如果当前单词的长度大于记录的长度,就把当前单词存下来,看我的例程吧。(我的算法判断是否是单词的依据是,只要不是英文字母的字符,就是单词分割符,比如空格,逗号,句号,叹号之类都是分隔符)

函数 isalphabetic 的作用是判断一个字符是否是26个英文字母中的一个, 函数 func 的作用是扫描输入字符串,找到最长的那个单词并打印。

#include <stdio.h>
#include <string.h>

int isalphabetic(char ch)
{
if ((ch >= 'a' && ch <= 'z')||(ch >= 'A' && ch <= 'Z'))
{
return 1;
}

return 0;
}

void func(char *pStr)
{
int i, j;
int max_len = 0, cur_len;
char result[80];
memset(result, 0, sizeof(result));
for(i = 0; pStr[i] != '\0'; )
{
if (isalphabetic(pStr[i]))
{
for (j = i; pStr[j] != '\0'; j++)
{
if (!isalphabetic(pStr[j]))
break;
}
if (max_len < (j - i))
{
max_len = (j - i);
strncpy(result, &pStr[i], (j-i));
result[j-i] = '\0';
}
i = j;
}
else
{
i++;
}
}
if (max_len > 0)
printf("Longest word is \'%s\'\n", result);
}

int main()
{
char c[80];
gets(c);
func(c);
return 0;
}


余干县19134133865: c语言编程在一个一直的字符串中查找最长的单词, -
康奋加迈: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62给出代码: #include <stdio.h>#include <string.h>intmain()...

余干县19134133865: c语言找到字符串中最长的单词 -
康奋加迈: 1. 给出代码: #include <stdio.h>#include <string.h>int main(){ char s[128]; char *p1, *p2; int max=0, len=0; printf("Input a string: "); gets(s);//此处用get更好,get会将空格也输入 p1=s; for (int i=0; i<=strlen(s); i++) { if ((s[i]==' ') || (s[i]=='\...

余干县19134133865: 用c语言,怎么输出字符串中最长的单词 -
康奋加迈: /*思路:定义一个指针p,指向每一个单词的开头定义一个指针q,用来循环遍历字符串定义一个指针s,用来指向长度最长的字符串 定义一个maxlen,用来存放最长的字符长度定义一个len,用来存放当前字符的长度 执行过程:首先,p,...

余干县19134133865: c语言编程 写一个函数,输入一行字符,将此字符串中最长的单词输出 -
康奋加迈: 楼上的何必不指出思路啊! 分析:首先输入的是一行字符吧,而要找出最长的单词,首先要做的就是统计出每个单词吧,统计单词可以根据空格来判断是否为一个单词,当不是空格时,变量累加(这个就是单词的长度啊),储存起来,开始下一个单词的统计!最后把每个单词的长度作比较,求出最大的一个就行了,在把这个长度的单词弄出来! 在编程之前,首先明确目标,理清思路,从上往下把需要的都写出来,这样长期的积累就能写出好程序了,祝愿楼主学有所成! PS:仅代表个人观点,如有错误,谢谢指出!还有楼主要找的是最长的单词,又不是找到ASC||最大的字母,不住楼上的例题,我有点理解不了,寡闻了吧,我!

余干县19134133865: C语言编程输入字符串,找出其中最长单词(要自定义函数) -
康奋加迈: char * max(char ** wordlib,int num)// num is the number of the words {int length = 0;int c = 0;int w = 0;int i = 0;int max;for(;c<n;c++){i = 0;w = 0;while(wordlib[c][i]!=0)//a word should be ended by a '\0'{i++;w++;}if(w> length){length = w;max = c;}}return wordlib[c];}

余干县19134133865: C语言 从键盘输入一行字符,输出其中最长单词? -
康奋加迈: 把fun函数中for(i=0; a[i]!='/0'; i++)改成for(i=0; a[i]!='\0'; i++).

余干县19134133865: c语言编程在一个已知的字符串中查找最长单词,假定字符串中只含字母和空格,空格用来分隔不同单词 -
康奋加迈: #include<stdio.h>#include<string.h>#define M 1000 int main() { int low; // 单词的起始下标 int high; // 单词的结束位置 int i; // 循环变量int count; // 统计最长单词的长度 int temp; // 中间变量int low_temp; int high_temp; char p[M]; // 存储有...

余干县19134133865: 用c语言怎么输出字符串中最长的单词? -
康奋加迈: //帮你改造一下:#include #include void main() { char c[50]; void f(char c[50]);//不设返回值了,直接用函数f打印结果. gets(c); f(c); } void f(char c[50]) { int len=0,lenth=0,i,flag=0; for(i=0;c[i]!='\0';i++) { if(c[i]!=' ') len++; if(c[i]==' ') {if(len>lenth) { lenth=...

余干县19134133865: c语言从一个字符串中提出最长的单词 -
康奋加迈: 代码就两个问题:1. 等到e的值后char b[e][80];这样声明二维数组b不行吧?声明数组的下标必须是常量,而e是变量!如果必须这么做,那得用malloc函数动态申请内存.2. 在f=0;前得加一句b[c][f]='\0';,使每个单词构成一个字符串!

余干县19134133865: C语言问题 求输出输入字符串中的最长单词? -
康奋加迈: #include #include int main() { char ch[100]; int i,j,len,maxlen,p; i=len=maxlen=0; printf("\n输入一串字符:"); gets(ch);//输入字符串,以空格分割单词 do { j=i;//新单词开始位置 for(len=0;ch[i]!=' '&&ch[i]!='\0';i++)//计算当前单词的长度 { len++; } ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网