U E D R , A S I H C RSS

문자열검색/조현태

C++

느낀점

단순 문자열검색 알고리즘이기때문에, 속도 향상과는 관계가 없다. 단지 찾아줄 뿐이다.

소스

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

using namespace std;

const int MAX_LONG=40;
const int TRUE=1;
const int FALSE=0;

void result_write( int , char* , char* );

int main()
{
	char x[MAX_LONG] = "His teaching method is very good."; 
	char y[MAX_LONG]={0,};
	cout << "현재의 문자열은 "<< x << "입니다.\n 검색하려는 문자열을 입력해주세요. \n >>>";
	cin >> y;
	int where_word=0;
	while (0!=x[where_word])
	{
		int such_word=0;
		while (x[where_word+such_word]==y[such_word] && 0!=y[such_word])
			++such_word;
		if (such_word>=strlen(y))
		{
			result_write( where_word , x , y );
			return TRUE;
		}
		++where_word;
	}
	result_write( -1 , x , y );
	return FALSE;
}

void result_write(int where, char *original, char *such_word)
{
	ofstream outputFile("result.out"); 
	outputFile << "자료 -> " << original << "\n찾을 문자열 -> " << such_word << "\n";
	if (-1==where)
		outputFile << "Not found!";
	else
		outputFile << "first found -> " << where;
	outputFile.close(); 
}

Erlang

느낀점


소스

  • pr_7.erl

-module(pr_7).
-export([findString/2]).
getText(Index) when 0 < Index -> Index - 1;
getText(Index) -> "Not found!".
findString(String, SubString) -> getText(string:rstr(String, SubString)).

  • Shell

11> c(pr_7).                      
./pr_7.erl:4: Warning: variable 'Index' is unused
{ok,pr_7}
12> pr_7:findString("abcd", "bc").
1
13> pr_7:findString("abcd", "cd").
2
14> pr_7:findString("abcd", "cb").
"Not found!"

나에게 할말

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:30
Processing time 0.0126 sec