Task: Search a list


C++: #include <string>
#include <algorithm>
#include <iterator>
#include <cstddef>
#include <exception>
#include <iostream>


class not_found: public std::exception
{
public:
  not_found(std::string const& s): text(s + " not found") {}
  char const* what() const throw() { return text.c_str(); }
  ~not_found() throw() {}
private:
  std::string text;
};


std::size_t get_index(std::string* haystack, int haystack_size, std::string needle)
{
  std::size_t index = std::find(haystack, haystack+haystack_size, needle) - haystack;
  if (index == haystack_size)
    throw not_found(needle);
  else
    return index;
}



template<typename FwdIter>
 typename std::iterator_traits<FwdIter>::difference_type fwd_get_index(FwdIter first, FwdIter last, std::string needle)
{
  FwdIter elem = std::find(first, last, needle);
  if (elem == last)
    throw not_found(needle);
  else
    return std::distance(first, elem);
}


template<typename InIter>
 typename std::iterator_traits<InIter>::difference_type generic_get_index(InIter first, InIter last, std::string needle)
{
  typename std::iterator_traits<InIter>::difference_type index = 0;
  while (first != last && *first != needle)
  {
    ++index;
    ++first;
  }
  if (first == last)
    throw not_found(needle);
  else
    return index;
}




std::string haystack[] = { "Zig", "Zag", "Wally", "Ronald", "Bush", "Krusty", "Charlie", "Bush", "Bozo" };


template<typename T, std::size_t sz> T* begin(T (&array)[sz]) { return array; }
template<typename T, std::size_t sz> T* end(T (&array)[sz]) { return array + sz; }
template<typename T, std::size_t sz> std::size_t size(T (&array)[sz]) { return sz; }


void test(std::string const& needle)
{
  std::cout << "-- C style interface --\n";
  try
  {
    std::size_t index = get_index(haystack, size(haystack), needle);
    std::cout << needle << " found at index " << index << "\n";
  }
  catch(std::exception& exc) 
  {
    std::cout << exc.what() << "\n";
  }

  std::cout << "-- generic interface, first version --\n";
  try
  {
    std::size_t index = fwd_get_index(begin(haystack), end(haystack), needle);
    std::cout << needle << " found at index " << index << "\n";
  }
  catch(std::exception& exc) 
  {
    std::cout << exc.what() << "\n";
  }

  std::cout << "-- generic interface, second version --\n";
  try
  {
    std::size_t index = generic_get_index(begin(haystack), end(haystack), needle);
    std::cout << needle << " found at index " << index << "\n";
  }
  catch(std::exception& exc) 
  {
    std::cout << exc.what() << "\n";
  }
}

int main()
{
  std::cout << "\n=== Word which only occurs once ===\n";
  test("Wally");
  std::cout << "\n=== Word occuring multiple times ===\n";
  test("Bush");
  std::cout << "\n=== Word not occuring at all ===\n";
  test("Goofy");
}








COBOL: 
       IDENTIFICATION DIVISION.
       PROGRAM-ID. Search-List.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  haystack-area.
           78  Haystack-Size VALUE 10.
           03  haystack-data.
               05  FILLER     PIC X(7) VALUE "Zig".
               05  FILLER     PIC X(7) VALUE "Zag".
               05  FILLER     PIC X(7) VALUE "Wally".
               05  FILLER     PIC X(7) VALUE "Ronald".
               05  FILLER     PIC X(7) VALUE "Bush".
               05  FILLER     PIC X(7) VALUE "Krusty".
               05  FILLER     PIC X(7) VALUE "Charlie".
               05  FILLER     PIC X(7) VALUE "Bush".
               05  FILLER     PIC X(7) VALUE "Boz".
               05  FILLER     PIC X(7) VALUE "Zag".

           03  haystack-table REDEFINES haystack-data.
               05  haystack   PIC X(7) OCCURS Haystack-Size TIMES
                   INDEXED BY haystack-index.

       01  needle             PIC X(7).

       PROCEDURE DIVISION.
       main.
           MOVE "Bush" TO needle
           PERFORM find-needle

           MOVE "Goofy" TO needle
           PERFORM find-needle

*          
           MOVE "Bush" TO needle
           PERFORM find-last-of-needle
           
           GOBACK
           .

       find-needle.
           SEARCH haystack
               AT END
                   DISPLAY needle " not found."

               WHEN haystack (haystack-index) = needle
                   DISPLAY "Found " needle " at " haystack-index "."
           END-SEARCH
           .

       find-last-of-needle.
           PERFORM VARYING haystack-index FROM Haystack-Size BY -1
               UNTIL haystack-index = 0
               OR haystack (haystack-index) = needle
           END-PERFORM

           IF haystack-index = 0
               DISPLAY needle " not found."
           ELSE
               DISPLAY "Found last of " needle " at " haystack-index "."
           END-IF
           .