U E D R , A S I H C RSS

문자열검색/허아영

.
.
.
,
.

. .ㅡㅠ
His teaching method is very good.
.
.( .)
, H, t, m, i, v, g .
, .
Hot , H His .
o i.. found 0 .
.

~cpp 
#include <stdio.h>

void exist_word(char x[40], int exist_str[10]); //x[i]  
int compare_str(char x[40], char search_str[15], int exist_str[10]); //  

int word_num = 1, search_str_num = 0;
int found = 0;	int temp = 0;

void main()
{
	char x[40] = "His teaching method is very good.";
	char search_str[15];
	int exist_str[10]; // exist_str[i] x i     
	FILE *fp;
	fp = fopen("result.out", "w");
	while(1){
		word_num = 1; search_str_num = 0; found = 0; temp = 0;
		printf(" ""EE""\n"); //.
		printf(" -> %s", x);
		printf("\n  -> ");
		scanf("%s", search_str);
		fprintf(fp," -> %s", x); //result.out .
		fprintf(fp, "\n  -> ");
		fprintf(fp, "%s", search_str);
		if(search_str[0] == 'E' && search_str[1] == 'E')
			break;
		exist_word(x, exist_str);
		found = compare_str(x, search_str, exist_str);
		
		switch(found){
		case 1:
			fprintf(fp, "\nfirst found -> %d\n\n", exist_str[word_num]+1);
			break;
		case 0:
			fprintf(fp, "\nNot found!\n\n");
			break;
		}
	}
	fclose(fp);
}

void exist_word(char x[40], int exist_str[10])
{
	int num = 2, x_n = 0;
	exist_str[1] = 0;
	while(x[x_n])
	{
		if(x[x_n] == ' ')
		{
			exist_str[num] = x_n + 1; //    .
			++num;
		}
		++x_n;
	}
}

int compare_str(char x[40], char search_str[15], int exist_str[10])
{	
	if(x[exist_str[word_num]] != search_str[search_str_num])
	{
		++word_num;	
		if(exist_str[word_num] >= 0)
			compare_str(x, search_str, exist_str);
		
	}else{
		temp = exist_str[word_num];
		while(search_str[search_str_num])
		{	
			if(x[temp] == search_str[search_str_num]){
				found = 1;
				++temp;
				++search_str_num;
			}else{
				found = 0;
				break;
			}
		}
	}
	return found;
}

C++ .

~cpp 
//cpp1.cpp
#include <iostream>
#include "class.h"

using namespace std;

void main()
{
	Search_ch c;
	c.input();
	c.search();
	c.print();

}
~cpp 
//cpp2.cpp
#include <iostream>
#include "class.h"

using namespace std;

void Search_ch::input()
{
	cin>>str;
	cin>>ch;
}

int Search_ch::search()
{
	int i, str_len, k;
	str_len = strlen(str);

	for(i = 0; i <= str_len-1; i++)
	{
		if(str[i] == ch)
		{
			k = i;
			break;
		}else{
			k = 0;
		}
	}
	return k;
}

void Search_ch::print()
{
	int found_num = search();
	if(found_num == 0){
		cout << "Not Found!!";
	}else if(0 < found_num && found_num < strlen(str)){
		cout << "Found!! >>>" <<found_num+1; 
	}
}
~cpp 
//class.h
class Search_ch 
{
private:

public:
	char str[20];
	char ch;
	void input();
	int search();
	void print();

};

한테 할

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