使用C中的strstr()查找最右边的出现位置

时间:2016-04-03 18:55:28

标签: c

我想找到另一个字符串中最右边的字符串,并返回它的位置。我看到三个帖子This one仅使用表格,但我想使用strstr()strstr()的问题是找到最左边的字符串,但是我想要最右边的,所以我试图做的是反转strstr()并使其从右到左工作一段代码。

int StrInd(char *MainStr, char *SecondStr){
 int i;
 i = strlen(MainStr);
 while (i >= 0) {
     if ( strstr( (MainStr + i)-1, SecondStr ) != NULL ) {  // found it
        return i;
     }
     i--;
 }
}

此代码的问题在于,如果字符串仅存在一次,则它不会出现。因此,例如当你给字符串“halolololele”,并搜索字符串“le”时,它将返回11,但是如果你搜索字符串“ha”,它将返回0。

代码中的缺陷在哪里,为什么它给出了不同的值?

以下是完整的代码。

int str_index(char *MainStr, char *SecondStr);

int main(int argc, char *argv[]) {

    int Ans, Seclen = 256,Mainlen = 256;
    char *SecondStr = malloc(Seclen);
    char *MainStr = malloc(Mainlen);

    printf("Give the first string: ");
    fgets(MainStr,Mainlen,stdin);
    printf("Give the second string: ");
    fgets(SecondStr,Seclen,stdin);

    Ans = StrInd(MainStr,SecondStr);
    printf("The position of the string is in: %d", Ans);

    free(MainStr);
    free(SecondStr);
    return 0;
}

int StrInd(char *MainStr, char *SecondStr){
    int i;
    i = strlen(MainStr);
    while (i >= 0) {
        if ( strstr( (MainStr + i)-1, SecondStr ) != NULL ) {  // found it
            return i;
        }
        i--;
    }
}

1 个答案:

答案 0 :(得分:0)

你的问题是示例返回位置11和0,但要保持一致,要么第一个示例应该是 index 10,要么第二个示例应该是 position 1。

这显示了一种查找字符串中最右边出现的子字符串的方法(从左侧开始)。只需将每次出现的指针(+1)反馈回#include <vector> #include <iterator> #include <iostream> #include <boost/graph/adjacency_list.hpp> #include <boost/graph/dominator_tree.hpp> #include <boost/property_map/property_map.hpp> struct GraphNode { explicit GraphNode(size_t i) : index {i} {} size_t index; }; using Graph = boost::adjacency_list<boost::listS, boost::listS, boost::bidirectionalS, GraphNode, boost::no_property>; namespace boost { template <> struct property_map<Graph, vertex_index_t> { typedef typename property_map<Graph, size_t GraphNode::*>::type type; typedef typename property_map<Graph, size_t GraphNode::*>::const_type const_type; }; static property_map<Graph, vertex_index_t>::type get(vertex_index_t, Graph& g) { return get(&GraphNode::index, g); } static property_map<Graph, vertex_index_t>::const_type get(vertex_index_t, Graph const& g) { return get(&GraphNode::index, g); } } using Vertex = boost::graph_traits<Graph>::vertex_descriptor; int main() { Graph g {}; const auto u = boost::add_vertex(GraphNode {0}, g); const auto v = boost::add_vertex(GraphNode {1}, g); const auto x = boost::add_vertex(GraphNode {2}, g); const auto y = boost::add_vertex(GraphNode {3}, g); const auto z = boost::add_vertex(GraphNode {4}, g); boost::add_edge(u, v, g); boost::add_edge(u, x, g); boost::add_edge(v, y, g); boost::add_edge(x, y, g); boost::add_edge(y, z, g); std::vector<Vertex> dom_pred(num_vertices(g), boost::graph_traits<Graph>::null_vertex()); auto index_map = boost::get(&GraphNode::index, g); // equivalent to vertex_index_t now auto dom_tree_pred_map (boost::make_iterator_property_map(std::begin(dom_pred), index_map)); // Run main algorithm boost::lengauer_tarjan_dominator_tree(g, u, dom_tree_pred_map); std::cout << "Result: "; for (auto v : dom_pred) { if (v == boost::graph_traits<Graph>::null_vertex()) std::cout << "(root) "; else std::cout << g[v].index << " "; } } ,就像这样。

Result: (root) 0 0 0 3 

计划会议:

strstr