easy :) for example using an IF clause...
String myname = "John";
String mysurname = "Doe";
if(myname.equalsIgnoreCase(mysurname))
{
// System.out.println("It seems you've got a weird name sir!");
}
else{ /* ignore this user */ }
You don't need a program to compare strings since std::string already provides support for all the comparison operators (<, <=, >, >=, == and !=). To roll your own you must first create a string class and then provide operator overloads for the comparison operators. To compare strings, start at the first character in each string and compare. So long as they remain equal, move onto the next character. The comparison ends as soon as any character differs. You need only compare these two characters to decide which string is the lesser. To perform a case insensitive comparison, copy the two characters and convert the copies to lower case (or upper case, it doesn't matter). Then compare the copies. Do this for each character as you compare them rather than converting the entire string.
Reverse the string and compare it to the original. If they match, then it is a palindrome.
#include<iostream> #include<string> #include<sstream> #include<vector> #include<conio.h> // required by getch() std::string input_string (std::string prompt) { while (true) { std::string input; std::cout << prompt; std::getline (std::cin, input); if (input.size()) return input; std::cout << "Invalid input. Empty strings are not permitted.\n"; } } size_t input_unsigned (std::string prompt) { while (true) { std::string input = input_string (prompt); std::stringstream ss; ss << input; size_t s; if (ss>>s && s) return s; std::cout << "Invalid input. Values must be non-zero.\n"; } } void string_length() { std::string str = input_string ("Enter the string you wish to know the length of:\n"); std::cout << "The string is " << str.size() << " characters long.\n" << std::endl; } void string_compare() { std::pair<std::string, std::string> strings; strings.first = input_string ("Enter the first string you wish to compare:\n"); strings.second = input_string ("Enter the second string you wish to compare:\n"); int compare = strings.first.compare (strings.second); std::cout << "The result of the camprison is: " << compare << '\n' << std::endl; } void string_reverse() { std::string str = input_string ("Enter the string you wish to reverse:\n"); for (size_t f=0, r=str.size()-1; f<r; ++f, --r) { char t = str[f]; str[f] = str[r]; str[r] = t; } std::cout << "The reversed string is:\n" << str << '\n' << std::endl; } void string_substring() { std::string str = input_string ("Enter a string:\n"); size_t pos = input_unsigned ("Enter the offset of the substring:\n"); size_t len = input_unsigned ("Enter the length of the substring:\n"); std::cout << "The substring is: " << str.substr(pos,len) << '\n' << std::endl; } void string_concatenation() { std::pair<std::string, std::string> strings; strings.first = input_string ("Enter the first string you wish to concatenate:\n"); strings.second = input_string ("Enter the second string you wish to concatenate:\n"); std::string concat = strings.first + strings.second; std::cout << "The result of the concatenation is: " << concat << '\n' << std::endl; } int main() { while (true) { std::cout << "MAIN MENU\n"; std::cout << "=========\n" << std::endl; std::cout << "1 - String length\n"; std::cout << "2 - String compare\n"; std::cout << "3 - String reverse\n"; std::cout << "4 - String substring\n"; std::cout << "5 - String concatenation\n"; std::cout << "X - Exit program\n"; #pragma warning(disable : 4996) switch ((char) getch()) { case ('1'): string_length(); break; case ('2'): string_compare(); break; case ('3'): string_reverse(); break; case ('4'): string_substring(); break; case ('5'): string_concatenation(); break; case ('x'): case ('X'): return 0; } } }
no, you cant. it only works on string
Prepare the string for processing: Remove all punctuation from the string (e.g., commas, hyphens, whitespace, etc). Convert to the same case (e.g., lower-case). Instantiate two pointers, one pointing at the first character, the other pointing at the last character. Process: If the two pointers are pointing at the same position or have crossed each other, the string is a palindrome. Otherwise, compare the characters being pointed at. If they are not equal, the string is not a palindrome. Otherwise, move both pointers one position towards the middle of the string and repeat the process.
There is no formula. You simply have to compare the character against each of the 5 vowels using a non-case-sensitive comparison. A common method is to construct a string containing only the 5 vowels and then test if the character is in the string.
"Trust thyself; every heart vibrates to that iron string." Iron string = belief in ones own thoughts, to be self confident.
Yes, the if the two string objects point to the same memory location. But "==" is not the best way to compare two string objects. Two strings can be compared using obj1.equals(obj2). This compares for the textual equal-ness of the two string objects.
how to compare two strings that take input from the user and compare it. For example: i give first string as "THE" and give second string as "HTE" then return "match" if i give first as"THE" nd second string as "EHI" then return "NOtMatch" witout using STRCMP ... please help me
performing string operation using pointers
With the string.h header file you can do special string functions such as ---- strcat() Sticks two strings together, one at the end of another. strncat() Sticks a given amount of characters from two strings together, one at the end of another. strchr() Returns the location of a character in a string from the beginning. strrchr() Returns the location of a character in a string from the end. strcmp() Compares 2 strings and returns the value 0 if both match. strcasecmp() Compares 2 strings ignoring case and returns the value 0 if both match. strncasecmp() Compares only a set number of characters in 2 strings ignoring case. strcpy() Copies one string into another. strncpy() Copies only a set number of characters from one string to another. strlen() Returns the length of the string (Except the final NULL character). The null character is \0 which dignifies the end of a string. strstr() Locates one string inside of another. Remember to use these you MUST include the string.h header file by typing #include <string.h>.
[ string toupper $str ] or [ string tolower $str ]