실습과제 모음

CPP 문자열 찾기 실습 답

TanSanC 2014. 4. 26. 12:55

#include <stdio.h>

#include <iostream>

#include <string>

using namespace std;

int main( )

{

    string object = "Hello World";

    int exist = 0;

    string searchChar;

    cin >> searchChar;

 

    int i;

    for ( i = 0 ; i < object.length() ; i++ )

    {

        if( searchChar.at(0) == object.at(i))

        {

            int count = 0;

            for( int j = 0 ; j < searchChar.length() ; j++ )

            {

                if( searchChar.at(j) == object.at(i+j))

                {

                    count++;

                }

                if( count == searchChar.length() -1 )

                {

                    exist = 1;

                }

            }

        }

    }

    // exist

    // 1        존재O

    // 0        존재X

    if( exist )

    {

        cout << searchChar << "가존재합니다." << endl;

    }

    else

    {

        cout << searchChar << "가존재하지않습니다." << endl;

    }

    return 0;

}