Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

c - 查找所有以字符X开头的后缀(Finding all suffix starting with a character X)

I need to find all suffix starting with a character X. For example, for int suffix (char str [], char c) when the word is ababcd and the letter b it should return:

(我需要找到所有以字符X开头的后缀。例如, for int suffix (char str [], char c)当单词ababcd和字母b都应返回时:)

babcd
bcd

and the number 2.

(和数字2)

This is my code:

(这是我的代码:)

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

int main()
{
    char c;
    char str[128];
    int counter=0;
    printf ("Please enter charachter and a string 
");
    scanf("%c %s",&c,str);
    counter = my_suffix(str,c);
    printf("The string has %d suffix 
",counter);
    return 0;
}

int my_suffix(char str[],char c) {
    int counter = 0;
    for (int i=0; i < strlen(str); i++)
    {
        if (str[i] == c)
        {   puts(str+i);
            counter++;
        }
    }
    return counter;
}

I couldn't find why it's not running,

(我找不到为什么它没有运行,)

Thanks!

(谢谢!)

  ask by Nadav Nusbaum translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

您的代码很好,您应该只在int main()之上编写以下方法

int my_suffix(char str[],char c){...}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...