列表

详情


QR9. filename

描述

Please create a function to extract the filename extension from the given path,return the extracted filename extension or null if none.

输入描述

输入数据为一个文件路径

输出描述

对于每个测试实例,要求输出对应的filename extension

示例1

输入:

Abc/file.txt

输出:

txt

原站题解

C 解法, 执行用时: 1ms, 内存消耗: 356KB, 提交时间: 2018-08-06

#include <stdio.h>

int main()
{
    char buf[1000];
    char ch;
    int index = 0;
    int flag_find = 0;
    
    while ((ch = getchar()) != '\n')
    {
        buf[index++] = ch;
    }
    buf[index] = 0;
    
    for (int i = 0; buf[i] != 0; i++)
    {
        if (buf[i] == '.')
        {
            if (buf[i + 1] == '/')
            {
                continue;
            }
            else 
            {
                flag_find = 1;
                for (int j = i + 1; buf[j] != 0; j++)
                {
                    printf("%c", buf[j]);
                }
                printf("\n");
                break;
            }
        }
    }
    
    if (!flag_find)
    {
        printf("null\n");
    }
}

C++ 解法, 执行用时: 1ms, 内存消耗: 368KB, 提交时间: 2017-09-05

# include<iostream>
# include<string>
# include<string.h>

 using namespace std ;

int main()
{
    string s ;
    getline(cin,s) ;
    int p ;
    p=s.find('.') ;
    if(p!=string::npos)
    {
        string c ;
        c=s.substr(p+1) ;
        cout<<c ;
    }
    else {cout<<"null" ;}
    return 0;
}

C++ 解法, 执行用时: 1ms, 内存消耗: 368KB, 提交时间: 2017-08-14

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char path[256];

int main(void) {
    
    scanf("%s", path);
    int i = 0, pos = 0;
    for (i = 0; i < strlen(path);  i++) {
        
        if (path[i] && path[i] == '.') {
            pos = i;
        	break;
        }
        //pos = -1;
    }
    if (pos) {
        while (path[pos+1] != '\0') {
            printf("%c", path[pos+1]);
            pos++;
        }
    } else {
        printf("null");
    }
}

C++ 解法, 执行用时: 1ms, 内存消耗: 372KB, 提交时间: 2017-10-24

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
    string str;
    cin >> str;
    if(str.size() != 0)
    {
    	auto ptr = find(str.cbegin(), str.cend(), '.');
        if(ptr != str.cend())
        {
       		++ptr;
    		for(; ptr != str.cend(); ++ptr)
        		cout << *ptr;
    		cout << endl;
        }
        else
            cout << "null";
    }
    return 0;
}

C++ 解法, 执行用时: 1ms, 内存消耗: 372KB, 提交时间: 2017-10-10

#include <iostream>
#include <string>
using namespace std;
int main()
    {
    string str;
    cin>>str;
    int num=str.find('.');
    if(num != string::npos)
        {
        char *p=&str[num+1];
    printf("%s",p);
    }
    else
    cout<<"null";
   	
    return 0;
}

上一题