首页 > 试题广场 >

字符串最后一个单词的长度

[编程题]字符串最后一个单词的长度
  • 热度指数:1490007 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
计算字符串最后一个单词的长度,单词以空格隔开,字符串长度小于5000。(注:字符串末尾不以空格为结尾

输入描述:

输入一行,代表要计算的字符串,非空,长度小于5000。



输出描述:

输出一个整数,表示输入字符串最后一个单词的长度。

示例1

输入

hello nowcoder

输出

8

说明

最后一个单词为nowcoder,长度为8   
推荐
/*使用动态数组来做,输入的字符串依次存入数组中,
最后返回数组中最后一个元素(字符串)的长度*/
#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main(){
	string input;
	vector<string>arr;
    while(cin>>input){
    	arr.push_back(input);
	}
	cout<<arr[arr.size()-1].length()<<endl;		
	return 0;
}

编辑于 2016-08-29 14:07:27 回复(93)
真的很省事
 #include<stdio.h>
  2 #include<string.h>
  3 int main(int argc,char *argv[]){
  4     //运行时输入./helloworld  hello world
  5     //argc = 3,argv[2] = world
  6     //输入hello world,输出argv[]i
  7     int a = 0;
  8     for(int i = 1; i < argc; i++){
  9         printf("%s ",argv[i]);
 10         a = strlen(argv[i]);
 11     }
 12     printf("\n");
 13     printf("%d\n",a);
 14     return 0;
 15 }

发表于 2024-04-24 11:58:55 回复(1)
#include<stdio.h>
#include<string.h>

int main(void) {
    char ch;
    int i, len=0;
    
    while(1){
    	ch=getchar();
		if(ch!=' '){
			len++;
		}
		else if(ch==' '){
			len=0;
		}    	
    	if(ch=='\n')
    	break;
	}
	
	
	printf("%d",len-1);
	
    return 0;
}
编辑于 2024-03-27 18:29:49 回复(0)
原有思路:
一组单词输入后,利用strlen()函数获得该组单词的长度,并从最后一位开始向前扫描,扫描到空格后停下,此长度为字符串最后一个单词的长度,需要注意的点有两个:
(1)不能利用scanf()函数,或者说,不能直接利用scanf()作为输入,因为其只能输入该字符串的第一个单词,可以改用fgets()
(2)需要考虑仅有一个单词的情况
#include <stdio.h>
#include <string.h>
int main() {
    int LastWordLen = 0;
    int WordsLen;
    char Words[5000];
    fgets(Words, 5000, stdin);
    // scanf("%s", Words);
    WordsLen = strlen(Words);
    // printf("%d\n", WordsLen);
    for (int i = 0; i < (WordsLen - 2); i++) {
        if (Words[WordsLen - 2 - i] != ' ')
            continue;
        else
            LastWordLen = i;
        break;
    }
    if (0 == LastWordLen)
        LastWordLen = WordsLen - 1;
    printf("%d\n", LastWordLen);
    return 0;
}
【他山之玉】
#include <stdio.h>
#include <string.h>

int main()
{
    char str[1000];
    int a=0,i=0;

    while(scanf("%s",str) != EOF)
    {}
    a=strlen(str);
    printf("%d",a);
}
1,scanf()函数,如果转换说明是%s的话,他的读取规则是,“读取除空白以外的所有字符串”。scanf()函数跳过空白开始读取第一个非空白字符,并保存非空白字符直到再次遇到空白字符。也就是说!!!!scanf()函数根据%s转换说明读取一个单词!!!
2,EOF,EOF是stdio.h文件里定义的特殊值,通常情况下#define指令会把EOF定义为“-1”,当scanf()读取到文件结尾时,就会返回EOF(如果是转换说明错误好像是会返回“0”)


编辑于 2024-03-25 19:45:39 回复(0)
#include <iostream>
#include <stdio.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

using namespace std;

int main(int argc, char** argv) {
    char a[5000]={0};
    gets(a);
    int l,i=0;
    l=strlen(a);
    while(a[l-1]!=' ')
    {
        i++;
        l--;
    }
    printf("%d",i);
    return 0;
}

编辑于 2024-03-07 17:02:32 回复(0)
#include <stdio.h>
#include <string.h>

int main() {
    char a[5000]={0};
    gets(a);
    int l,i=0;
    l=strlen(a);
    while(a[l-1]!=' ')
    {
        i++;
        l--;
    }
    printf("%d",i);
    return 0;
}


发表于 2024-03-05 22:13:15 回复(0)
#include <stdio.h>
#include <string.h>

int main()
{   char str[5002]={0};
    gets(str);
    int len=strlen(str);
    int result=0;
    for(int i=len-1;i>=0;i--)/*逆序遍历,没读到空格时累加数加一,读到空格时结束累加*/
    {
        if(str[i]!=' ')
        {
            result++;
        }
        else
        {
            break;
        }
    }
    printf("%d",result);
    return 0;
}
发表于 2024-01-03 17:59:03 回复(0)
IDE上正常运行,网页上调试也没问题但是网页的实际输出就是0,真是搞不懂
#include <stdio.h>

#define MAX_LEN 5000

int main() {
    int i, ch, num = 0;
    while ((ch = getchar()) != '\n' && i < MAX_LEN) {
        i++;
        num++;
        if (ch == ' ') num = 0;
    }
    printf("%d", num);
}

发表于 2023-12-26 15:25:05 回复(0)
#include <stdio.h>
#include <string.h>
int main() {
    char input_string[5002];
    scanf("%[^\n]", input_string);
    int len = strlen(input_string);
    int result = 0;
    for (int i = len - 1; i >= 0; i--) {
        if (input_string[i] != ' ') {
            result ++;
        } else if (input_string[i] == ' ') {
            break;
        }
    }
    printf("%d", result);
    return 0;
}
如果只是输出长度的话,那么逆序遍历到第一个空格的时候说明已经得到了长度。
发表于 2023-11-08 11:03:02 回复(0)
#include <stdio.h>
#include <string.h>
 
int main() {

    char str[1000];
    int a=0;
    while (scanf("%s", str) != EOF) {
    }
    a=strlen(str);
    printf("%d",a);
    return 0;
}
发表于 2023-10-27 21:56:01 回复(1)
#include <stdio.h>
#include <string.h>
int main(void)
{
    char buf[5000] = { 0 };
    scanf("%[^\n]",buf);
    int len = 0;
    for (int i = strlen(buf) - 1; i >= 0; i--)
    {
        if (buf[i] == ' ')
        {
            break;
        }
        else
        {
            len++;
        }
    }
    printf("%d",len);
    return 0;
}
发表于 2023-09-20 09:23:03 回复(0)
#include <stdio.h>
#include <string.h>
//统计空格数,遍历至最后一个空格时Out
int main() {
    int count,total;
    count=total=0;
    char s[5000]={0};
    gets(s);
    for(int i=0;i<strlen(s);i++)
    {
        if(s[i]==' ')
            total++;
    }
    // printf("%d %d\n",total,(int)strlen(s));
    if(total==0)
        printf("%d",(int)strlen(s));
    else
        for(int i=0;i<strlen(s);i++)
        {
            if(s[i]==' ')
                count++;
            if(count==total)
            {
                printf("%d",(int)strlen(s)-i-1);
                break;
            }    
        }
    return 0;
}
发表于 2023-08-28 21:36:08 回复(0)
#include<iostream>

#include<string>

#include<vector>

using namespace std;

int main(){

    string s;

    vector<string>v;

    while(cin>>s){

        v.push_back(s);

    }

    int l = v.size();

    for(int i =l-1;i>0;i--){

        cout<<v[i]<<' ';

    }

    cout<<v[0]<<endl;

    return 0;

}
发表于 2023-08-05 22:06:30 回复(0)
这是为什么报红,启动不了
发表于 2023-04-21 20:48:10 回复(2)
/*循环输入每个单词,单词由空格间隔,用getchar存储这个空格
最后单词的下一个输入不为空格,判断getchar,此时即可计算当前输入的单词的长度,并退出循环*/
#include <string.h>
int main() {
    char s[20];
    char c;
    int count;
    int i = 0;
    while (scanf("%s", s)) {
        c = getchar();
        if (c != ' ') {
            count = strlen(s);
            break;
        }
    }
    printf("%d", count);
}

发表于 2023-04-18 16:22:50 回复(0)
#include <stdio.h>
#include <string.h>

int main(void)
{
    char str[5000];
    while (~scanf("%s", str)) {}
    printf("%d", strlen(str));
}

发表于 2023-04-11 22:13:24 回复(3)
/**  题目:HJ1 字符串最后一个单词的长度
  *  思路:从后往前遍历字符串找空格位置,最后单词长度 = 总长 - 空格位置下标 + 1;若无空格,下标置为 -1
  *  难点:使用scanf("%s", str) 输入字符串遇到空格会停止输入;无空格情况
  */
#include <stdio.h>
#include <string.h>

int main() {
    char str[5000];
    int len;
    int i;

    gets(str);
    len = strlen(str);
    i = len;

    //  查找最后一个空格的位置,如果没有空格, i == -1
    for (; i >= 0; i--) {
        if (str[i] == ' ')
            break;
    }

    printf("%d\n", len - i - 1);

    return 0;
}

发表于 2023-03-26 17:18:54 回复(0)

问题信息

难度:
62条回答 388603浏览

热门推荐

通过挑战的用户

查看代码