-
Notifications
You must be signed in to change notification settings - Fork 15
Strings and String Functions
A string refers to a continuous stream of characters that collectively form a single unit. Most modern day programming languages have some way of representing strings. C++ has a String class, Java has a String object and so does python. So why are strings so important? Strings provide a way for us to represent a stream character data in a meaningful manner.
#include<string.h>
using namespace std;
int main()
{
string x = "foo bar";
}class Main
{
public static void main(String args[])
{
String x;
}
}Variables in python don't explicitly need to be given types and thus can be declared with just the name like so,
string = "Hello World"In C++ Strings can be input by using cin for a single word, or using getline for multiple words. An example of that would be:
string x;
x = "Hello world";
cin >> x;
getline(cin,x);Here x = "Hello world" is used to assign a value directly, cin >> x takes only a single word and discards the rest of the input, where as getline(cin,x) takes multiple words until the \n character is encountered.
Remember to discard the rogue \n character after a string is accepted, cause if it is followed by another string acceptance statement it might end up with the wrong input.
Input in java is very similar to input in C++ with cin replaced with sc.next where sc is an object of the Scanner class.
Inputs in python are also quite straight forward, Use raw_input() in python2 and input() in python3.
Concatenation is the process of appending one string at the end of another, this is pretty similar in all three languages, it can be achieved with the help of the '+' operator, which in this case performs the concatenation operation.
In C++ and Java the above operation would be performed like so,
string1 = string1 + string2;The only thing different in python would be the lack of a semi colon
A substring is a smaller string that is contained within a string. Accessing a substring with indices.
string b = a.substr(i,j);Here a is the string whose substring you want, and i,j are the beginning and length of the substring respectively. Ommiting j outputs the string from i to the end.
Thus for a = "Hello World!" and i = 1, j = 4 will give us b = ello.
In java the substring method can be used to get the substring of a string. it works very similar to the substr function of C++ taking in two variables i and j Only here i marks the beginning and j marks the end of the substring ie, the substring method prints i to j-1 elements.
String b = a.substring(i,j);if a = "Hello world!", and i = 1, j = 4 will give us b = ell.
In python strings are very easy to work with, a substring can be accessed by placing them between sqare braces ie,
a[i:j] will give us the substring from i to j-1, and either i or j or both can be ommited to get either the string from i or upto j or the whole string itself.
ie,
a = 'Hello world!'
a[:2]
a[2:]
a[:]Here these 3 will have the values of 'He', 'llo world!' and 'Hello world!' respectively.
a[i:j:step] will give us the substring from i to j-1, with characters only after every step value. For example,
a[i:j:2] will give the string from i to j-1 with step 2, or alternate characters from i to j. This make reversing a list very easy for us. Reversing a list can be done by l[::-1]. This means the whole list with -1 step or reverse step.
So now that we have spoken about substrings for a while, how exactly would you go about finding a substring? There are multiple algorithms to find a given substring within a string, the most straight forward one of those will be discussed here.
Say you want to find a substring W within a string S, what would be your first line of thought. compare the first characters of W and S, check if they match if yes compare the next characters, if not compare first character of W with the second character of S and so on.
Algorithmically this can be written as
for i in range(len(S)-len(W)):
for j in range(len(W)):
if S[i+j]!=W[j]:
break
if j == len(W)-1:
return trueUnlike substrings, that are a continuous set of characters in a string, subsequences are the strings that are formed by selectively picking out characters from a string while keeping their relative order intact. Some important things that are searched for in strings are longest common subsequences, total number of subsequences, etc.
A palindrome is a string that reads the same both forwards and backwards, some examples of palindromes in the english language are words like : racecar, level, noon, etc.
Two words are said to be anagrams of each other if the letters in one of the words can be repostioned to get the other word. Some examples of such words are : Listen - Silent, Earth - Heart, etc
int length(void)
The length function or the size function return the length of the string object.
To use these we can write
string a ="Hello world!";
cout<< a.length()<<endl;
cout<<a.size()<<endl;The output of this will be 12 in both the cases.
const char* c_str(void)
The c_str function returns the pointer to the character array or the string object, ie it returns a const char * that points to the beginning of the character array.
string a = "Hello world!";
const char *b = a.c_str();
cout<< b;notice here that the returned value is a constant char * and thus cannot be edited.
int find(string)
This can be used to find the first occurance of the input string in the base string.
Strings in java are an immutable type and thus cannot be changed. Though some functions can be called on them, lets look at a few of them
char charAt(int)
This method can be used to get the character that is present at the given index. the index must be a valid index.
String a = "Hello world!";
System.out.println(a.charAt(0));The above code outputs the character 'H'
bool equals(String)
This method checks if the two objects are equal or not.
Note: equals is used over the '==' sign because the '==' sign compares the references and if the references point to different objects which have the same value it still evaluates to false.
int indexOf(char/String)
This method returns the first occurence of the character/substring that is passed.
char[] toCharArray(void)
Works similar to the c_str function in C++