Home » Strings in Functional Programming

Strings in Functional Programming

by Online Tutorials Library

Strings in Functional Programming

The string is defined as the group of the characters, which includes the space. The string is known as the one-dimensional character array. The string always ended by the “” or the NULL character. String is known as the predefined class. Most of the programming languages like C, C++, Erlang, Java supports String.

The below table shows how the string “Java” will look in memory.

Creating a string in C++

The below program will show how to create the string in C++. C++ is known as the object-oriented programming language.

The output of the above program is shown below:

Today is: Holiday   

String in Erlang

The below code shows the creation of the string in Erlang. Erlang is known as the functional programming language.

The output of the above program is just like, as shown below:

"Today is Independence"  

String operations in C++

Different languages support different methods in the strings. The below table shows the predefined string methods supported by the C++ are:

Sr.No. Methods and Description
1 Strcpy(t1,t2)
It copies the string t2 into string t1
2 Strcat(t1,t2)
It adds the string t2 at the end of t1
3 Strlen(t1) It provides the length of the string t1
4 Strcmp(t1,t2)
It returns 0 when string t1 & t2 are the same
5 Strchr(t1,ch)
It returns a pointer to the first occurrence of character ch in string t1
6 Strstr(t1,t2)
It returns a pointer to the first occurrence of string t2 in string t1

The below program shows how we can use the above methods in C++

Output of the above program will look like as shown below:

strcpy(st3, st1)   :  Today is   strcat(st1, st2)   :  Today is Rakshabandhan   strlen(st1)         :  19   

String operations in Erlang

String operations in Erlang are as shown below:

S.No. Method & Description
1 len(l1)
len(l1) method will return the characters present in the string.
2 equal(l1,l2)
equal(l1,l2) will compare the two strings and return the result as true when the strings are equal else this will return the false.
3 concat(l1,l2)
concat(l1,l2) method is used to add the string l2 at the end of the string l1.
4 str(l1,ch)
str(l1,ch) method return the position of the character ch present in the string l1.
5 str (l1,l2)
str(l1,l2) method returns the index position of l2 in string l1
6 substr(l1,l2,num)
substr(l1,l2,num) method returns the string l2 from the string l1 which is based on the starting position & number of characters in the starting position
7 to_lower(l1)
to_lower(l1) method returns string in lower case

Below program shows how we can use the above methods in Erlang.

Output of the above program will look like as shown below:

"Today is Monday"   

Next Topic#

You may also like