Friday, January 31, 2014

String operations


Two string comparison:

Header file:

class String_operations
{
 public:
     String_operations()
     {}
      bool string_compare(char* p_str1, char* p_str2 );
};
Cpp File:

bool String_operations::string_compare(char *p_str1, char *p_str2)
{
    bool status = true;
    while(*p_str1 != '\0' || *p_str2 != '\0' )
    {
        if(*p_str1 == *p_str2)
        {
         p_str1++;
         p_str2++;
         status = true;
        }
        else
        {
            status = false;
            break;
        }
    }
    return status;
}

Main method:

int main()
{
    String_operations* p_str_oper = new String_operations();
    bool status = p_str_oper->string_compare("He", "Hello");
    if(status)
    {
        printf("Both strings are equal");
    }
    else
        printf("Both strings are not equal");
    return 0;
}









int main()
{
    String_operations* p_str_oper = new String_operations();
    bool status = p_str_oper->string_compare("Hello", "Hello");
    if(status)
    {
        printf("Both strings are equal");
    }
    else
        printf("Both strings are not equal");
    return 0;
}





























No comments: