우선 다음 소스는 Java용 소스를 //// 표시가 붙은 줄 한 줄만 수정하여 Groovy영 소스로 고친 것이다.

[파일명:  testStringFindApp.groovy]------------------------------------------------
import java.util.*;

public class TestStringFindApp {

    public static void main(String[] args) {
        String[] words = [ "하나", "둘", "셋", "넷", "다섯", "여섯" ] as String[];   ////
        int where;

        System.out.print("array: ");
        printArray(words);
        where = find(words, "셋");
        if (where > 0) {
            System.out.print("발견!  ");
            System.out.println("Next word of 셋 in array: " + words[where+1]);
        }

        System.out.println("Sorting...");
        Arrays.sort(words);

        System.out.print("array: ");
        printArray(words);
        where = find(words, "셋");
        if (where > 0) {
            System.out.print("발견!  ");
            System.out.println("Next word of 셋 in array: " + words[where+1]);
        }
    }

    static int find(String[] arr, String s) {
     for (int i = 0; i < arr.length; i++) {
      if (arr[i].indexOf(s) >= 0)
       return i;
     }
     return -1;
    }

    static void printArray(String[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length - 1; i++) {
            System.out.print(arr[i] + ", ");
        }
        if ( arr.length > 0)
            System.out.print(arr[arr.length - 1]);
        System.out.println("]");
    }
}
------------------------------------------------


실행> groovy testStringFindApp.groovy
array: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in array: 넷
Sorting...
array: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in array: 여섯



그리고 다음 소스는 위의 Groovy용 소스를 더 Grovy 소스 코드답게 짧게 만든 것이다.

[파일명:  testStringFindApp.groovy]------------------------------------------------
def find(String[] arr, String s) {
   for (int i = 0; i < arr.length; i++) {
     if (arr[i].indexOf(s) >= 0)
      return i;
    }
    return -1;
}

def printArray(String[] arr) {
    print("[");
    for (int i = 0; i < arr.length - 1; i++) {
        print(arr[i] + ", ");
    }
    if (arr.length > 0)
        print(arr[arr.length - 1]);
    println("]");
}

String[] words = [ "하나", "둘", "셋", "넷", "다섯", "여섯" ] as String[]
int where

print("array: ")
printArray(words)
where = find(words, "셋")
if (where > 0) {
    print("발견!  ")
    println("Next word of 셋 in array: " + words[where+1])
}

println("Sorting...")
Arrays.sort(words)

print("array: ")
printArray(words)
where = find(words, "셋")
if (where > 0) {
    print("발견!  ")
    println("Next word of 셋 in array: " + words[where+1])
}
------------------------------------------------


실행> groovy testStringFind.groovy
array: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in array: 넷
Sorting...
array: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in array: 여섯


크리에이티브 커먼즈 라이선스
Creative Commons License

 


Posted by Scripter
,


[파일명:  TestStringFindInList.java]------------------------------------------------
import java.util.*;

public class TestStringFindInList {

    public static void main(String[] args) {
        String[] data = { "하나", "둘", "셋", "넷", "다섯", "여섯" };
        ArrayList<String> words = new ArrayList<String>();
        for (int i = 0; i < data.length; i++) {
            words.add(data[i]);
        }
        int where;

        System.out.print("vector: ");
        printArray(words);
        where = find(words, "셋");
        if (where > 0) {
            System.out.print("발견!  ");
            System.out.println("Next word of 셋 in list: " + words.get(where+1));
        }

        System.out.println("Sorting...");
        Collections.sort(words);

        System.out.print("vector: ");
        printArray(words);
        where = find(words, "셋");
        if (where > 0) {
            System.out.print("발견!  ");
            System.out.println("Next word of 셋 in list: " + words.get(where+1));
        }
    }

    static int find(ArrayList<String> arr, String s) {
     for (int i = 0; i < arr.size(); i++) {
      if (arr.get(i).indexOf(s) >= 0)
       return i;
     }
     return -1;
    }

    static void printArray(ArrayList<String> arr) {
        System.out.print("[");
        for (int i = 0; i < arr.size() - 1; i++) {
            System.out.print(arr.get(i) + ", ");
        }
        if ( arr.size() > 0)
            System.out.print(arr.get(arr.size() - 1));
        System.out.println("]");
    }
}
------------------------------------------------


컴파일> javac testStringFindInList.java

실행> java TestStringFindInList
list: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in list: 넷
Sorting...
list: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in list: 여섯


크리에이티브 커먼즈 라이선스
Creative Commons License

Posted by Scripter
,


[파일명:  TestStringFindInVector.java]------------------------------------------------
import java.util.*;

public class TestStringFindInVector {

    public static void main(String[] args) {
        String[] data = { "하나", "둘", "셋", "넷", "다섯", "여섯" };
        Vector<String> words = new Vector<String>();
        for (int i = 0; i < data.length; i++) {
            words.add(data[i]);
        }
        int where;

        System.out.print("vector: ");
        printArray(words);
        where = find(words, "셋");
        if (where > 0) {
            System.out.print("발견!  ");
            System.out.println("Next word of 셋 in vector: " + words.elementAt(where+1));
        }

        System.out.println("Sorting...");
        Collections.sort(words);

        System.out.print("vector: ");
        printArray(words);
        where = find(words, "셋");
        if (where > 0) {
            System.out.print("발견!  ");
            System.out.println("Next word of 셋 in vector: " + words.elementAt(where+1));
        }
    }

    static int find(Vector<String> arr, String s) {
     for (int i = 0; i < arr.size(); i++) {
      if (arr.elementAt(i).indexOf(s) >= 0)
       return i;
     }
     return -1;
    }

    static void printArray(Vector<String> arr) {
        System.out.print("[");
        for (int i = 0; i < arr.size() - 1; i++) {
            System.out.print(arr.elementAt(i) + ", ");
        }
        if ( arr.size() > 0)
            System.out.print(arr.elementAt(arr.size() - 1));
        System.out.println("]");
    }
}
------------------------------------------------


컴파일> javac TestStringFindInVector.java

실행> java TestStringFindInVector
vector: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in vector: 넷
Sorting...
vector: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in vector: 여섯


크리에이티브 커먼즈 라이선스
Creative Commons License
Posted by Scripter
,


[파일명:  TestStringFindApp.java]------------------------------------------------
import java.util.*;

public class TestStringFindApp {

    public static void main(String[] args) {
        String[] words = { "하나", "둘", "셋", "넷", "다섯", "여섯" };
        int where;

        System.out.print("array: ");
        printArray(words);
        where = find(words, "셋");
        if (where > 0) {
            System.out.print("발견!  ");
            System.out.println("Next word of 셋 in array: " + words[where+1]);
        }

        System.out.println("Sorting...");
        Arrays.sort(words);

        System.out.print("array: ");
        printArray(words);
        where = find(words, "셋");
        if (where > 0) {
            System.out.print("발견!  ");
            System.out.println("Next word of 셋 in array: " + words[where+1]);
        }
    }

    static int find(String[] arr, String s) {
     for (int i = 0; i < arr.length; i++) {
      if (arr[i].indexOf(s) >= 0)
       return i;
     }
     return -1;
    }

    static void printArray(String[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length - 1; i++) {
            System.out.print(arr[i] + ", ");
        }
        if ( arr.length > 0)
            System.out.print(arr[arr.length - 1]);
        System.out.println("]");
    }
}
------------------------------------------------


컴파일> javac testStringFindApp.java

실행> java TestStringFindApp
array: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in array: 넷
Sorting...
array: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in array: 여섯



크리에이티브 커먼즈 라이선스
Creative Commons License

 

Posted by Scripter
,


[파일명:  testStringFindInList.cpp]------------------------------------------------
#include <iostream>
#include <functional>
#include <algorithm>
#include <string>
#include <list>

using namespace std;

struct SData {
    SData(string s) : m_s(s) {}
    string m_s;
};

namespace std {
    template <> struct greater<SData*> {
        bool operator()(SData const* p1, SData const* p2) {
            //Defined like less for ascending sorting
            if(!p1)
                return true;
            if(!p2)
                return false;
            return p1->m_s < p2->m_s;
        }
    };
};

// Predicate class
class MyPred {
private:
    std::string     m_strForFind;

public:
    // for find_if
    void setQuery(std::string str) {
        m_strForFind = str;
    };

    // operator override for find_if
    bool operator()(SData const* p) {
        if(!p)
            return false;
        int n = (p->m_s).find(m_strForFind);
        int m = (p->m_s).length();
        return n >= 0 && n < m;
    };
};

void printList(list<SData*> lst);

int main() {
    list<SData*> mylist;
    mylist.push_back(new SData("하나"));
    mylist.push_back(new SData("둘"));
    mylist.push_back(new SData("셋"));
    mylist.push_back(new SData("넷"));
    mylist.push_back(new SData("다섯"));
    mylist.push_back(new SData("여섯"));

    cout << "list: ";
    printList(mylist);

    MyPred pred;
    pred.setQuery("셋");
    list<SData*>::iterator it;
    it = find_if(mylist.begin(), mylist.end(), pred);
    if (it != mylist.end()) {
        cout << "발견!  ";
        cout << "Next word of 셋 in list: " << (*(++it))->m_s << endl;
    }

    // Sorting list in ascending order
    cout << "Sorting..." << endl;
    mylist.sort(greater<SData*>());

    cout << "list: ";
    printList(mylist);

    it = find_if(mylist.begin(), mylist.end(), pred);
    if (it != mylist.end()) {
        cout << "발견!  ";
        cout << "Next word of 셋 in list: " << (*(++it))->m_s << endl;
    }

    for (it = mylist.begin(); it != mylist.end(); it++)
        delete *it;

    return 0;
}

void printList(list<SData*> lst) {
    list<SData*>::iterator it;
    list<SData*>::iterator it2;
    cout << "[";
    for (it = lst.begin(); it != lst.end(); it++) {
     it2 = it;
        advance(it2, 1);
        if (it2 == lst.end())
            break;
        cout << (*it)->m_s << ", ";
    }
    if (lst.size() > 0)
        cout << (*it)->m_s;
    cout << "]" << endl;
}
------------------------------------------------


Visual C++의 경우:
컴파일> cl /EHsc testStringFindInList.cpp
또는
Borland C++의 경우:
컴파일> bcc32 testStringFindInList.cpp
또는
Cywin이나 Msys의  g++의 경우:
컴파일> g++ -o testStringFindInList testStringFindInList.cpp

실행> testStringFindInList
list: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in list: 넷
Sorting...
list: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in list: 여섯


Cywin이나 Msys의  g++의 경우:
실행> ./testStringFindInList
list: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in list: 넷
Sorting...
list: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in list: 여섯



크리에이티브 커먼즈 라이선스
Creative Commons License

 


Posted by Scripter
,


[파일명:  testStringFindInVector.cpp]------------------------------------------------
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>

using namespace std;

void printArray(vector<string>  arr);

int main() {
    int SIZE = 6;
    string data[] = { "하나", "둘", "셋", "넷", "다섯", "여섯" };
    vector<string> words(&data[0], &data[SIZE]);
    vector<string>::iterator it;

    /////////////////////////////////
    // words[0] = "하나";
    // words[1] = "둘";
    // words[2] = "셋";
    // words[3] = "넷";
    // words[4] = "다섯";
    // words[5] = "여섯";

    cout << "vector: ";
    printArray(words);

    it = find(words.begin(), words.end(), "셋");
    if (it != words.end()) {
        cout << "발견!  ";
        cout << "Next word of 셋 in vector: " << *(++it) << endl;
    }

    cout << "Sorting..." << endl;
    sort(words.begin(), words.end());

    cout << "vector: ";
    printArray(words);

    it = find(words.begin(), words.end(), "셋");
    if (it != words.end()) {
        cout << "발견!  ";
        cout << "Next word of 셋 in vector: " << *(++it) << endl;
    }
     return 0;
}

void printArray(vector<string> v) {
    cout << "[";
    for (int i = 0; i < v.size() - 1; i++) {
        cout << v[i] << ", ";
    }
    if (v.size() > 0)
        cout << v[v.size() - 1];
    cout << "]" << endl;
}
------------------------------------------------


Visual C++의 경우:
컴파일> cl /EHsc testStringFindInVector.cpp
또는
Borland C++의 경우:
컴파일> bcc32 testStringFindInVector.cpp
또는
Cywin이나 Msys의  g++의 경우:
컴파일> g++ -o testStringFindInVector testStringFindInVector.cpp

실행> testStringFindInVector
vector: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in vector: 넷
Sorting...
vector: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in vector: 여섯


Cywin이나 Msys의  g++의 경우:
실행> ./testStringFindInVector
vector: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in vector: 넷
Sorting...
vector: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in vector: 여섯



크리에이티브 커먼즈 라이선스
Creative Commons License

 

Posted by Scripter
,


[파일명:  testStringFindApp.cpp]------------------------------------------------
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

void printArray(string arr[], int size);

int main() {
   // string words[] = { "one", "two", "three","four", "five"};
   int SIZE = 6;
   string words[] = { "하나", "둘", "셋", "넷", "다섯", "여섯" };
   string* where;

   cout << "array: ";
   printArray(words, SIZE); 
   where = find(words, words + SIZE, "셋");
   if (where - words < SIZE)
       cout << "발견!  ";
   if (where != &words[SIZE])
       cout << "Next word of 셋 in array: " << (*++where) << endl;

   cout << "Sorting..." << endl;
   sort(words, words + SIZE);

   cout << "array: ";
   printArray(words, SIZE); 
   where = find(words, words + SIZE, "셋");
   if (where - words < SIZE)
       cout << "발견!  ";
   if (where != &words[SIZE])
       cout << "Next word of 셋 in array: " << (*++where) << endl;

    return 0;
}

void printArray(string arr[], int size) {
    cout << "[";
    for (int i = 0; i < size - 1; i++) {
       cout << arr[i] << ", ";
    }
    if (size> 0)
        cout << arr[size - 1];
    cout << "]" << endl;
}
------------------------------------------------


Visual C++의 경우:
컴파일> cl /EHsc testStringFindApp.cpp
또는
Borland C++의 경우:
컴파일> bcc32 testStringFindApp.cpp
또는
Cywin이나 Msys의  g++의 경우:
컴파일> g++ -o testStringFindApp testStringFindApp.cpp

실행> testStringFindApp
array: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in array: 넷
Sorting...
array: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in array: 여섯


Cywin이나 Msys의  g++의 경우:
실행> ./testStringFindApp
array: [하나, 둘, 셋, 넷, 다섯, 여섯]
발견!  Next word of 셋 in array: 넷
Sorting...
array: [넷, 다섯, 둘, 셋, 여섯, 하나]
발견!  Next word of 셋 in array: 여섯



크리에이티브 커먼즈 라이선스
Creative Commons License

 

Posted by Scripter
,


[파일명:  testSort.boo]------------------------------------------------
import System
import System.Collections

def printArray(arr as Array):
    Console.Write("[")
    for i in range(0, len(arr) - 1):
        s = "${arr[i]}, "
        Console.Write(s)
    if len(arr) > 0:
        Console.Write("{0}", "${arr[len(arr) - 1]}")
    Console.WriteLine("]")

arr = array(String, argv.Length)
for i in range(0,  argv.Length):
    arr[i] = argv[i]

Array.Sort(arr)
printArray(arr)
------------------------------------------------


실행> booi testSort.boo one two thee four five
[five, four, one, three, four]

실행> booi testSort.boo 하나 둘 셋 넷 다섯
[넷, 다섯, 둘, 셋, 하나]

 

크리에이티브 커먼즈 라이선스
Creative Commons License
Posted by Scripter
,


[파일명:  testSortApp.cpp]------------------------------------------------
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;

void printArray(array<String^>^ arr);

int main(int argc, char *argv[]) {
    array<String^>^ arr = gcnew array<String^>(argc - 1);
    for (int i = 1; i < argc; i++) {
        arr[i-1] = gcnew String(argv[i]);
    }
    Array::Sort(arr);
    printArray(arr);
}
  
void printArray(array<String^>^ arr) {
    Console::Write("[");
    for (int i = 0; i < arr->Length - 1; i++) {
        Console::Write("{0}, ", arr[i]);
    }
    if (arr->Length > 0)
        Console::Write("{0}", arr[arr->Length - 1]);
    Console::WriteLine("]");
}
------------------------------------------------


Visual C++의 경우:
컴파일> cl /clr testSortApp.cpp

실행> testSort one two thee four five
[five, four, one, three, four]

실행> testSortApp 하나 둘 셋 넷 다섯
[넷, 다섯, 둘, 셋, 하나]


 

크리에이티브 커먼즈 라이선스
Creative Commons License

 

Posted by Scripter
,


[파일명:  testSort.lua]------------------------------------------------
function find(arr, s)
    for i = 1, #arr do
     if string.find(arr[i], s) ~= nil then
      return i
        end
    end
    return -1;
end

function printArray(arr)
    io.write("[")
    for i = 1, #arr - 1 do
        io.write(arr[i] .. ", ")
    end
    if #arr > 0 then
        io.write(arr[#arr])
    end
    io.write("]\n")
end

words = { "하나", "둘", "셋", "넷", "다섯", "여섯" }

io.write("list: ")
printArray(words)
where = find(words, "셋")
if where > 0 then
    io.write("발견!  ")
    print("Next word of 셋 in list: " .. words[where+1])
end

print("Sorting...")
table.sort(words)

io.write("list: ")
printArray(words)
where = find(words, "셋")
if where > 0 then
    io.write("발견!  ")
    print("Next word of 셋 in list: " .. words[where+1])
end
------------------------------------------------


실행> lua testSort.lua one two three four five
[five, four, one, three, two]

실행> lua testSort.lua 하나 둘 셋 넷 다섯
[넷, 다섯, 둘, 셋, 하나]

실행> lua testSort.lua 자전차 자전거 전동차 전차 전기자동차
[자전거, 자전차, 전기자동차, 전동차, 전차]


 

크리에이티브 커먼즈 라이선스
Creative Commons License
Posted by Scripter
,