C语言 统计歌词中的单词个数,并找出最短和最长的单词(必须用指针)求帮忙,感谢!!!
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言 统计歌词中的单词个数,并找出最短和最长的单词(必须用指针)求帮忙,感谢!!!相关的知识,希望对你有一定的参考价值。
统计歌词中的单词个数,并找出最短和最长的单词。
歌词:Daddy finger, Daddy finger, where are you? Here I am. Here I am. How do you do? Mammy finger, Mammy finger, where are you? Here I am. Here I am. How do you do? Brother finger, Brother finger, where are you? Here I am. Here I am. How do you do? Sister finger, Sister finger, where are you? Here I am. Here I am. How do you do? Baby finger, Baby finger, where are you? Here I am. Here I am. How do you do?
设计函数实现:
函数int gecifun( char *geci, char *strmax, char *strmin)——实现求geci中的单词个数并返回,strmax和strmin中分别存放最长和最短单词;
主函数——调用函数gecifun并输出。
程序输出:歌词中共有85个单词。
歌词中最长的单词为Brother,最短的单词为I。
主函数——调用函数gecifun并输出。 参考技术B #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 500
int gecifun(char *geci, char *strmax, char *strmin)
char temp[30];//存储每个单词数组
int num=0,i,word=0;//单词数量和每个单词数组下标
for(i=0;(geci[i])!='\0';i++)
if(geci[i]==' '||geci[i]==',' || geci[i]=='.' || geci[i]=='?') //单词分隔符
if (word != 0)
temp[word]='\0';
if (num==1)//获取第一个单词的时候,将第一个单词复制给最大和最小
strcpy(strmax,temp);
strcpy(strmin,temp);
else//第二个单词之后比较字符串长度之后进行复制
if (strlen(temp)>=strlen(strmax))
strcpy(strmax,temp);
if (strlen(temp)<=strlen(strmin))
strcpy(strmin,temp);
num++;//单词数量递增
word = 0;//单词数组下标置零
else
temp[word++] = geci[i];//每个单词按位置赋值
return num;
int main()
int acount;
char lstr[30],sstr[30];
char song[N] = "Daddy finger, Daddy finger, where are you? Here I am. Here I am."
"How do you do? Mammy finger, Mammy finger, where are you? Here I am. Here "
"I am. How do you do? Brother finger, Brother finger, where are you? Here "
"I am. Here I am. How do you do? Sister finger, Sister finger, where are you?"
"Here I am. Here I am. How do you do? Baby finger, Baby finger, where are you?"
"Here I am. Here I am. How do you do?";
acount=gecifun(song,lstr,sstr);
printf("歌词中共有%d个单词。歌词中最长的单词为%s,最短的单词为%s。\n",acount,lstr,sstr);
return 0;
最短和最长的城市名称
【中文标题】最短和最长的城市名称【英文标题】:shortest and longest city name 【发布时间】:2019-12-25 06:49:03 【问题描述】:查询 STATION 中两个城市的 CITY 名称最短和最长,以及它们各自的长度(即:名称中的字符数)。如果有不止一个最小或最大的城市,请选择按字母顺序排列的第一个城市。
请问这个问题的答案是什么?
SELECT CITY,
LENGTH(CITY)
FROM STATION
ORDER BY CITY ASC
FETCH FIRST 1 ROWS ONLY ;
为什么此查询不适用于获取最短长度城市?
【问题讨论】:
这不起作用,因为您是通过city
订购的,而您可能打算通过length(city)
订购。
【参考方案1】:
我会在这里使用ROW_NUMBER
:
WITH cte AS (
SELECT s.*, ROW_NUMBER() OVER (ORDER BY LENGTH(CITY), CITY) rn_first,
ROW_NUMBER() OVER (ORDER BY LENGTH(CITY) DESC, CITY) rn_last
FROM STATION s
)
SELECT CITY, LENGTH(CITY) AS CITY_LENGTH
FROM cte
WHERE 1 IN (rn_first, rn_last)
ORDER BY LENGTH(CITY);
这将生成一个包含两个记录的结果集,最短的城市名称出现在最前面,最长的出现在最后。
【讨论】:
是的,我在黑客等级上使用 row_number 做到了这一点。Oracle 也只接受获取前 1 行,但这在黑客等级上不起作用。【参考方案2】:我最喜欢在 Oracle 中执行此操作的方法是使用 keep
关键字:
select min(city) keep (dense_rank first order by length(city) asc) as shortest_city,
min(city) keep (dense_rank first order by length(city) desc) as longest_city,
min(length(city)) as min_len,
max(length(city)) as max_len
from station;
根据我的经验,keep
的性能非常好,因此如果它在大数据上具有最佳性能,我不会感到惊讶。
【讨论】:
我不明白。为什么会缺少城市名称? @BarbarosÖzhan 。 . .谢谢你。我确定了答案。【参考方案3】:感谢您的回复。我会检查“保留”一次。 我是这样解决的。有没有其他的可能来优化它或者让它更简单?
**
select city,length(city) as "SL"
from (select city from station order by city asc)
where length(city) in (select min(length(city)) from station) and rownum=1;
select city,length(city) as "LL"
from (select city from station order by city asc)
where length(city) in (select max(length(city)) from station)
order by city desc;`
**
【讨论】:
【参考方案4】:对于 MS SQL Server,解决方案如下。
Declare @Small int
Declare @Large int
select @Small = Min(Len(CITY )) from STATION
select @Large = Max(Len(CITY )) from STATION
;with cte1 as(
select Top 1 CITY as SmallestCity,Len(CITY ) as Minimumlength from STATION where Len(CITY ) = @Small Order by CITY Asc
),
cte2 as(
select Top 1 CITY as LargestCity,Len(CITY ) as MaximumLength from STATION where Len(CITY ) = @Large Order by CITY Asc
)
select * from cte1
union
select * from cte2
它的输出看起来像这样, Out put of query
【讨论】:
【参考方案5】:我编写的代码适用于 Microsoft SQL Server
select city, len(city) from station where len(city) = (select max(len(city)) from station);
select city, len(city) from station where len(city) = (select min(len(city)) from station) order by city desc offset 2 rows
【讨论】:
正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。【参考方案6】:你需要修改你的ORDER BY
:
SELECT CITY, LENGTH(CITY)
FROM STATION
ORDER BY LENGTH(CITY) DESC, CITY
FETCH FIRST 1 ROWS ONLY ;
这将给出最长名字的答案。对于最短的名称,只需将ORDER BY
更改为使用ASC
为LENGTH(CITY)
。
【讨论】:
要求要求两个城市名称,而不仅仅是一个。 我确实阅读了该部分,但用户没有指定它必须在一个查询中。不过,您的回答会解决这个问题。【参考方案7】:SELECT CITY,
LENGTH(CITY) as length
FROM CITY
ORDER BY length ASC limit 1;
【讨论】:
limit
选项 ORDER
子句在 Oracle 中不存在。
虽然这个答案可能是正确的。仅代码答案很少有帮助。请评论您的代码并解释此代码如何解决问题。【参考方案8】:
选择城市,长度(城市)
从车站
WHERE LENGTH(CITY) IN (SELECT min(LENGTH(CITY))
FROM STATION
)
AND ROWNUM
联合
选择城市,长度(城市)
从车站 WHERE LENGTH(CITY) IN (SELECT max(LENGTH(CITY))
FROM STATION )
AND ROWNUM
按城市 ASC 排序;
【讨论】:
【参考方案9】:SELECT top 1 City,LEN(City) as Citylength into #tmp
FROM STATION
WHERE LEN(City)=(SELECT MIN(LEN(City)) FROM STATION )
order by City asc
SELECT top 1 City,LEN(City) as Citylength into #tmp1
FROM STATION
WHERE LEN(City)=(SELECT MAX(LEN(City)) FROM STATION )
order by City
select * from #tmp
union
select * from #tmp1
【讨论】:
以上是关于C语言 统计歌词中的单词个数,并找出最短和最长的单词(必须用指针)求帮忙,感谢!!!的主要内容,如果未能解决你的问题,请参考以下文章