Functional Programming - Strings



A string is a group of characters including spaces. We can say it is a one-dimensional array of characters which is terminated by a NULL character (‘\0’). A string can also be regarded as a predefined class which is supported by most of the programming languages such as C, C++, Java, PHP, Erlang, Haskell, Lisp, etc.

The following image shows how the string "Tutorial" will look in the memory.

Strings

Create a String in C++

The following program is an example that shows how to create a string in C++, which is an object-oriented programming language.

#include <iostream> 
using namespace std; 

int main () {    
   char greeting[20] = {'H', 'o', 'l', 'i', 'd', 'a', 'y', '\0'}; 
   cout << "Today is: "; 
   cout << greeting << endl; 
   return 0; 
} 

It will produce the following output −

Today is: Holiday 

String in Erlang

The following program is an example that shows how to create a string in Erlang, which is a functional programming language.

-module(helloworld).  
-export([start/0]).   
start() -> 
   Str = "Today is: Holiday",  
   io:fwrite("~p~n",[Str]). 

It will produce the following output −

"Today is: Holiday" 

String Operations in C++

Different programming languages support different methods on strings. The following table shows a few predefined string methods supported by C++.

S.No. Method & Description
1

Strcpy(s1,s2)

It copies the string s2 into string s1

2

Strcat(s1,s2)

It adds the string s2 at the end of s1

3

Strlen(s1)

It provides the length of the string s1

4

Strcmp(s1,s2)

It returns 0 when string s1 & s2 are same

5

Strchr(s1,ch)

It returns a pointer to the first occurrence of character ch in string s1

6

Strstr(s1,s2)

It returns a pointer to the first occurrence of string s2 in string s1

The following program shows how the above methods can be used in C++ −

#include <iostream> 
#include <cstring> 
using namespace std; 

int main () {   
   char str1[20] = "Today is "; 
   char str2[20] = "Monday"; 
   char str3[20]; 
   int  len ;  
   strcpy( str3, str1); // copy str1 into str3 
   cout << "strcpy( str3, str1) : " << str3 << endl;  

   strcat( str1, str2); // concatenates str1 and str2 
   cout << "strcat( str1, str2): " << str1 << endl;  

   len = strlen(str1);  // String length after concatenation 
   cout << "strlen(str1) : " << len << endl; 
   return 0; 
}    

It will produce the following output −

strcpy(str3, str1)   :  Today is 
strcat(str1, str2)   :  Today is Monday 
strlen(str1)         :  15 

String Operations in Erlang

The following table shows a list of predefined string methods supported by Erlang.

S.No. Method & Description
1

len(s1)

Returns the number of characters in the given string.

2

equal(s1,s2)

It returns true when string s1 & s2 are equal else return false

3

concat(s1,s2)

It adds string s2 at the end of string s1

4

str(s1,ch)

It returns index position of character ch in string s1

5

str (s1,s2)

It returns index position of s2 in string s1

6

substr(s1,s2,num)

This method returns the string s2 from the string s1 based on the starting position & number of characters from the starting position

7

to_lower(s1)

This method returns string in lower case

The following program shows how the above methods can be used in Erlang.

-module(helloworld).  
-import(string,[concat/2]).  
-export([start/0]).  
   start() ->  
   S1 = "Today is ",  
   S2 = "Monday",  
   S3 = concat(S1,S2),  
   io:fwrite("~p~n",[S3]). 

It will produce the following output −

"Today is Monday" 
Advertisements