- int lastIndexOf(char*s)
- {
- int str_len = strlen(str);
- int s_len = strlen(s);
- int result = -1;
- char*ss = "";
- char rev_str[256] = "";
- char rev_s[256] = "";
-
- strncpy_s(rev_str, str, sizeof(rev_str)-1);
- _strrev(rev_str);
- strncpy_s(rev_s, s, sizeof(rev_s)-1);
- _strrev(rev_s);
-
- rev_str[strlen(rev_str)] = '\0';
- rev_s[strlen(rev_s)] = '\0';
-
-
- if ((ss = strstr(rev_str, rev_s)) != NULL)
- {
- int ss_len = strlen(ss);
- int a = str_len - ss_len;
- result = str_len - (a + 1);
- }
-
- return result;
- }
8. substring() :
This function takes an argument of char pointer. This function returns the part of the string from the specific position.
- char* substring(int startIndex)
- {
- char*result = (char*)malloc(sizeof(char)* 100);
- int str_len = strlen(str);
- int i = 0;
-
- if (startIndex > 0)
- {
- while (i < str_len)
- {
- result[i] = str[startIndex + i];
- i++;
- }
- }
- result[i] = '\0';
- return result;
- }
Now lets see some character functions provided in java. I am creating a class Character and defining the static functions in it.
- class Character
- {
- public:
-
- };
Lets see some character functions used in java. Define all the following functions in above class.
1. isLetter() :
This function takes an argument of char. This function returns true if the given character is an alphabet letter, otherwise returns false.
- static char* isLetter(char ch)
- {
- static char*result = "false";
-
- for (char c = 'a'; c <= 'z'; c++)
- {
- for (char c1 = 'A'; c1 <= 'Z'; c1++)
- {
- if (ch == c || ch == c1)
- {
- result = "true";
- }
- }
- }
-
- return result;
- }
2. isDigit():
This function takes an argument of char and returns true if given character is digit otherwise returns false.
- static char* isDigit(char ch)
- {
- static char*result = "false";
-
- for (char c = '0'; c <= '9'; c++)
- {
- if (ch == c)
- {
- result = "true";
- }
- }
-
- return result;
- }
3. isUpperCase():
This function takes an argument of char. This function returns true if given character is in uppercase otherwise returns false. isLowerCase() function is same only just change in for loop character values to (a,z).
- static char* isUpperCase(char ch)
- {
- static char*result = "false";
-
- for (char c = 'A'; c <= 'Z'; c++)
- {
- if (ch == c)
- {
- result = "true";
- }
- }
-
- return result;
- }
Example
To use all defined functions in above,you must create the object of String class. Remember you have to pass the array of character value to the constructor. To handle exception in Visual Studio,create the object of class String in following way. So do like this
- char mystr[] = "Java Programming";
- String s1(mystr);
Instead of,
- String s1("Java Programming");
We have defined the static methods in Character class, so those methods can be called without creating the object of Character class. Use like this Character::isLetter('T')
Here's my example to implement these all functions. Download the source code.
- #include "stdafx.h"
- #include<conio.h>
- #include"javastringfunctions.h"
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- char mystr[] = "Java Programming";
- String s1(mystr);
-
- std::cout << "\t----String class functions----";
- std::cout << "\n\t1] getValue() = " << s1.getValue();
- std::cout << "\n\t2] charAt() = " << s1.charAt(5);
- std::cout << "\n\t3] compareTo() = " << s1.compareTo("java program");
- std::cout << "\n\t4] endsWith() = " << s1.endsWith("ming");
- std::cout << "\n\t5] equals() = " << s1.equals("java programming");
- std::cout << "\n\t6] equalsIgnoreCase() = " << s1.equalsIgnoreCase("JAVA PROGRAMMING");
- char a[100]="";
- s1.getChars(1, 14, a);
- std::cout << "\n\t7] getChars() = " << a;
-
-
-
- s1.setValue("The horse can run faster than human being");
- std::cout << "\n\n\tNow string is '" << s1.getValue()<<"'";
- std::cout << "\n\n\t8] indexOf() = " << s1.indexOf("horse");
- std::cout << "\n\t9] lastIndexOf() = " << s1.lastIndexOf("faster");
- std::cout << "\n\t10] length() = " << s1.length();
- std::cout << "\n\t11] replace() = " << s1.replace("horse", "dog");
-
- char mystr2[] = "The Elephant is bigger than ant";
- String s2(mystr2);
- std::cout << "\n\n\tNow string is '" << s2.getValue() << "'";
- std::cout << "\n\n\t12] Reverse() = " << s2.reverse();
-
- s2.reverse();
-
- std::cout << "\n\t13] startsWith() = " << s2.startsWith("The");
- std::cout << "\n\t14] substring() = " << s2.substring(13);
- std::cout << "\n\t15] toLowerCase() = " << s2.toLowerCase();
- std::cout << "\n\t16] toUpperCase() = " << s2.toUpperCase();
-
- char mystr3[] = " C/C++ Programming ";
- String s3(mystr3);
- std::cout << "\n\n\tNow string is '" << s3.getValue() << "'";
- std::cout << "\n\n\t17] trim() = " << s3.trim();
-
-
- std::cout << "\n\n\t----Character class functions----";
- std::cout << "\n\t1] isLetter() = " << Character::isLetter('T');
- std::cout << "\n\t2] isDigit() = " << Character::isDigit('6');
- std::cout << "\n\t3] isUpperCase() = " << Character::isUpperCase('c');
- std::cout << "\n\t4] isLowerCase() = " << Character::isLowerCase('W');
- std::cout << "\n\t5] isWhiteSpace() = " << Character::isWhitespace('c');
- std::cout << "\n\t isWhiteSpace() = " << Character::isWhitespace(' ');
-
- String s4("C:\\Visual Studio Projects\\My String Functions\\My String Functions\\My String Functions.cpp");
- std::cout << "\n\n\tFilename = " << s4.substring(s4.lastIndexOf("\\") + 1);
-
- std::cout << "\n\n\t----External String functions----";
- char b[] = "Hello";
- char c[] = "World";
- std::cout << "\n\ttoUpperCase() = " << Str_toUpperCase(b);
- std::cout << "\n\treverse() = " << Str_reverse(c);
- _getch();
- return 0;
- }
Here's the output of above program.