首页 > 试题广场 >

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

[编程题]字符串最后一个单词的长度
  • 热度指数:1585463 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
\hspace{15pt}对于给定的若干个单词组成的句子,每个单词均由大小写字母混合构成,单词间使用单个空格分隔。输出最后一个单词的长度。

输入描述:
\hspace{15pt}在一行上输入若干个字符串,每个字符串代表一个单词,组成给定的句子。
\hspace{15pt}除此之外,保证每个单词非空,由大小写字母混合构成,且总字符长度不超过 10^3


输出描述:
\hspace{15pt}在一行上输出一个整数,代表最后一个单词的长度。
示例1

输入

HelloNowcoder

输出

13

说明

\hspace{15pt}在这个样例中,最后一个单词是 \texttt{ ,长度为 13
示例2

输入

A B C D

输出

1
推荐
/*使用动态数组来做,输入的字符串依次存入数组中,
最后返回数组中最后一个元素(字符串)的长度*/
#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>
#include <stdlib.h>

int main()
{
    char str[100];
    fgets(str, 100, stdin);
    int len =strlen(str);
    int laststrlen = 0;
    int i = len-1;
    while (str[i] == ' ' || str[i] == '\0' || str[i] == '\n')
    {
        i--;
    }
    while (str[i] != ' '&&i>=0)
    {
        laststrlen++;
            i--;
    }
    printf("%d", laststrlen);
    return 0;
}
发表于 2025-03-29 18:02:04 回复(0)
#include <stdio.h>


//输入后开始计数,遇到空格且后边还有字符串则count清零
int main() {
    
    int count = 0;
    char ch = '\n';
    int flag = 0;//区分空格后边还有没有字符 
    while ((ch=getchar())!='\n') {
        if ((ch!=' ')&&flag==0) {
            count++;
        }else if ((ch!=' ')&&flag==1) {
            count=1;
            flag = 0;
        }
        else if ((ch==' ')&&flag==0) {
            flag = 1;
        }
    }
    printf("%d",count);
    return 0;
}

发表于 2025-03-25 10:36:28 回复(0)
#include <stdio.h>

int main() {
    int a, b;
    while (scanf("%d %d", &a, &b) != EOF) { // 注意 while 处理多个 case
        // 64 位输出请用 printf("%lld") to
        printf("%d\n", a + b);
    }
    return 0;
}
发表于 2025-03-11 20:45:22 回复(0)
#include<stdio.h>
#include <stdlib.h>

int main()
{
    char stance[1000];
    int lastlen=0;
    gets(stance);//用scanf在输入出现空格时会异常,故用gets(不过有溢出风险
    int len=strlen(stance);
    for(int i=len-1;i>=-1;i--)
    {
        if(i!=-1)//判断输入的是句子(带有空格的)还是单词
        {
          if(stance[i]==' ')//检测空格
          {
            lastlen=len-i-1;
            break;
          } 
        }
        if(i==-1)
        {
            lastlen=len;
        }
    }
    printf("%d\n",lastlen);
    return 0;
}


发表于 2025-03-04 01:14:43 回复(0)
#include <stdio.h>
#include <string.h>
//直接用strtok计算最后一个单词的长度
int main() {
    int len = 0;
    char sentence[1000]; // 假设句子长度不超过1000个字符
    while (fgets(sentence, sizeof(sentence), stdin)) { // 读取输入
        char* token = strtok(sentence, " ");
        while (token != NULL) {
            len = strlen(token)-1;
            token = strtok(NULL, " ");
        }
        printf("%d\n", len); // 输出结果
        return 0;
    }
}
发表于 2025-02-27 17:04:17 回复(0)
#include <iostream>  
#include <sstream>  
#include <string>  

using namespace std;  

int main() {  
    string input;  
    getline(cin, input); // 读取整行输入  
    // 使用字符串流分割单词  
    stringstream ss(input);  
    string word;  
    string lastWord; // 用于存储最后一个单词  

    // 遍历输入的单词  
    while (ss >> word) {  
        lastWord = word; // 更新最后一个单词  
    }  

    // 输出最后一个单词的长度  
    cout  << lastWord.length() << endl;  

    return 0;  
}
发表于 2025-02-16 18:01:20 回复(0)
#include<stdio.h>
#include<string.h>
int str_len(char *arr);
int main(){
    int i;
    char str[1000];
    gets(str);
    i=str_len(str);
    printf("%d\n",i);
    return 0;

}
int str_len(char *arr){
    int i=0;
    char *p=arr;

    while(*p !='\0'){
        p++;
    }
    while((*p !=' ')){
       
        p--;
        i++;
        if(p==&arr[0]){
            i++;
            break;
        }
       
    }

    return i-1;
}
发表于 2025-02-10 20:17:54 回复(0)
#include <stdio.h>
#include <string.h>

int main(){
    char s[1000];
    int i,j=0,m;
    gets(s);
    int len=strlen(s);
    for(i=len-1;i>=0;i--){
        if(s[i]==' '){
            for(m=i+1;m<=len-1;m++){j++;}
            break;
        }
        if(i==0)
        j=len;
        }
   
    /*while(1){
        ch=getchar();
        if(ch=='\n')
            break;
            else if(ch==' '){
                n=0;
            }
            else{
                n++;
            }*/
    printf("%d\n",j);
    return 0;
}
发表于 2024-12-30 21:10:13 回复(0)
#include <stdio.h>
#include <string.h>
int main() {
   char x;
   int count=0,y=0;
   char k[100]={0};
   if (fgets(k, sizeof(k), stdin) != NULL) {
        //printf("You entered: %s",k);
    }
    //printf("\n");
    for(int i = 0;i<sizeof(k);i++)
    {
        count++;
        if(k[i] == ' ')
        {
            count=0;
        }
        if(k[i+1] == '\n')
        {
            break;
        }
    }
    printf("%d",count);
}
发表于 2024-11-19 23:19:13 回复(0)
#include <stdio.h>
#include <string.h>
int main() {
    int a, b,i,j,k,m,n=0;
    char c[5000],t='0';
    gets(c);
    m=strlen(c);
    for(i=0;i<m;i++){
        if(c[i]==' '){
            t=c[i];
            
            n=i;
        }
    }
    if(t=='0'){
        k=m-n;
    }
    else{
        k=m-n-1;
    }
    
    printf("%d\n",k);
    
    return 0;
}

发表于 2024-11-09 09:13:13 回复(0)
#include <stdio.h>
#include <string.h>

int main() {
    char arr[5000];
    fgets(arr, sizeof(arr), stdin);
    printf("%d\n", (int)strlen(strrchr(arr, ' ')) - 2);
    return 0;
}
发表于 2024-11-08 12:53:04 回复(0)
 无循环版本。注意fgets返回的字符串含有换行符,所以长度要减一;此外strrchr返回的字符串以空格为开头,所以长度也要减一。
#include <stdio.h>
#include <string.h>

char str[5001];

int main()
{
    fgets(str,sizeof(str),stdin);
    char* p=strrchr(str,' ');
    int l=p?strlen(p)-1:strlen(str);
    printf("%d",l-1);
    return 0;
}


发表于 2024-08-17 05:20:55 回复(0)
#include <stdio.h>

int main() {
    int count = 0;
    char tmp;
    while (scanf("%c", &tmp) && tmp != '\n') {
        count = tmp != ' '? count + 1 :  0;
    }
    printf("%d", count);
    return 0;
}
发表于 2024-08-10 12:28:02 回复(0)

#include<stdio.h>
int main(){
   char ch=0
   int str_len=0
   while(1){
    ch=getchar();
    if(ch=='\n')
        break;
       else if(ch==''){
           str_len=0
           }else{
           str_len++;
       }
   }
    print("%d\n",str_len)
    return 0
    }


发表于 2024-07-26 13:54:34 回复(0)
//题目提到了不以空格结尾,而且标点符号不是单词,所以统统不用考虑
#include <stdio.h>
int main() {
    char ch = 0;
    int str_len = 0;
    while (1) {
        ch = getchar();
        if (ch == '\n')
            break;
        else if (ch == ' ') {
            str_len = 0;
        } else {
            str_len++;
        }
    }
    printf("%d\n", str_len);
    return 0;
}
发表于 2024-07-23 16:28:23 回复(0)
#include <stdio.h>
#include <string.h>
int main()
{    char a[5000];
    int num;
    while(scanf("%s",a)!=EOF){
        num=strlen(a);}
    printf("%d\n",num);
    return 0;
}
发表于 2024-07-17 12:16:00 回复(0)
直接通过EOF找最后输入的一段字符串,然后再利用strlen()函数算长度就行了
#include <stdio.h>
#include <string.h>
#define N 5000

int main() {
    int a, b;
    char str[N];
    while (scanf("%s", &str) != EOF) { // 注意 while 处理多个 case
        a = 0;
    }
    a = strlen(str);
    printf("%d", a);
    return 0;
}


发表于 2024-07-10 01:18:28 回复(0)
#include <stdio.h>
int main() {
     char a;
    int num=0;
    while(scanf("%c",&a)&&a!='\n'){
        if(a==' '){num=0;}
        else {num++;}
    }
    printf("%d",num);
    return 0;
}
有没有更快的方法
发表于 2024-06-06 21:30:38 回复(0)
#include <stdio.h>
#include <string.h>

int main() {
    char sentence[5000];
    while(scanf("%s", sentence) != EOF){}
    printf("%d", strlen(sentence));
    return 0;
}
发表于 2024-05-31 15:10:11 回复(0)