Computer Programming - Strings

During our discussion about characters in computer programming, we learnt that character data type deals with a single character and you can assign any character from your keyboard to a character type variable.
Now, let's move a little bit ahead and consider a situation where we need to store more than one character in a variable. We have seen that C programming does not allow to store more than one character in a character type variable. So following statements are invalid in C programming and produce syntax error:
char ch1 = 'ab';
char ch2 = '10';
We also have seen how we can store more than one value of similar data type in a variable using array concept. If recap then, here is the syntax to store and print 5 numbers in an array of int type:
#include <stdio.h>

main()
{
    int number[5] = {10, 20, 30, 40, 50};
    int i = 0;
        
    while( i < 5 )
    {
        printf("number[%d] = %d\n", i, number[i] );
        i = i + 1;
   }
}
When the above code is compiled and executed, it produces the following result:
number[0] = 10
number[1] = 20
number[2] = 30
number[3] = 40
number[4] = 50
Now, let's define an array of 5 characters in the similar way as we did for numbers and try to print them:
#include <stdio.h>

main()
{
    char ch[5] = {'H', 'e', 'l', 'l', 'o'};
    int i = 0;
        
    while( i < 5 )
    {
        printf("ch[%d] = %c\n", i, ch[i] );
        i = i + 1;
   }
}
Here, we used %c to print character value. When the above code is compiled and executed, it produces the following result:
ch[0] = H
ch[1] = e
ch[2] = l
ch[3] = l
ch[4] = o
If you are done with the above example, then I think you understood about strings in C programming, because strings in C are represented as arrays of characters. C programming simplified the assignment and printing of strings. Let's check same example once again with simplified syntax:
#include <stdio.h>

main()
{
    char ch[5] = "Hello";
    int i = 0;
    
    /* Print as a complete string */
    printf("String = %s\n", ch);  
    
    /* Print character by character */
    while( i < 5 )
    {
        printf("ch[%d] = %c\n", i, ch[i] );
        i = i + 1;
   }
}
Here, we used %s to print full string value using array name ch, which is actually beginning of the memory address holding ch variable as shown below:
String Presentation in C/C++ Though it's not visible from the above examples, but internally C program assigns null character '\0' as the last character of every string. This indicates the end of the string and it means if you want to store a 5 character string in an array then you must define array size of 6 as a good practice, though C does not complain about it.
Now if the above code is compiled and executed, it produces the following result:
String = Hell
ch[0] = H
ch[1] = e
ch[2] = l
ch[3] = l
ch[4] = o

Basic String Concepts

Based on the above discussion we can conclude the following important points to remember about strings in C programming language:
  • Strings in C are represented as arrays of characters.
  • We can constitute a string in C programming by assigning character by character into an array of characters.
  • We can constitute a string in C programming by assigning a complete string enclosed in double quote.
  • We can print a string character by character using array subscript or a complete string by using array name without subscript.
  • Though it's not visible from the above examples, but internally C program assigns null character '\0' as the last character of every string. This indicates the end of the string and it means if you want to store a 5-character string in an array then you must define array size of 6 as a good practice, though C does not complain about it.
  • Most of the programming languages provide built-in functions to manipulate strings, i.e., you can concatenate strings, you can search from a string, you can take sub string from the string. For a detail you can check detailed tutorial for C or other programming languages.

Strings in Java

Though you can use character array to store strings but Java is an advanced programming language and its designers tried to provide additional functionality like Java provides string as a built-in data type like any other data type. So it means you can define strings directly instead of defining them array of characters.
Following is the equivalent program written in Java programming language. Java programming makes use of new operator to create string variable as shown below in the program:
You can try to execute the following program to see the output:
public class DemoJava
{    
   public static void main(String []args) 
   {
       String str = new String("Hello");  
       
       System.out.println( "String = " + str );
   }
}
When above program is executed, it produces the following result:
String = Hello

Strings in Python

Creating strings in Python is as simple as simply assigning a string into a Python variable using single or double quote as shown below:
Following is a simple program, which creates two strings and print them using print() function:
var1 = 'Hello World!'
var2 = "Python Programming"

print "var1 = ", var1
print "var2 = ", var2
When above program is executed, it produces the following result:
var1 =  Hello World!
var2 =  Python Programming
Python does not support a character type; these are treated as strings of length one, thus also considered a substring.
To access substrings, use the square brackets for slicing along with the index or indices to obtain your substring. Following is a simple example:
var1 = 'Hello World!'
var2 = "Python Programming"

print "var1[0]: ", var1[0]
print "var2[1:5]: ", var2[1:5]
When the above code is executed, it produces the following result:
var1[0]:  H
var2[1:5]:  ytho 
A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. You already have seen various functions like printf() and main(). These are called built-in functions provided by the language itself, but we can write our own functions as well and this tutorial will teach you how to write and use those functions in C programming language.
Good thing about functions is that they are famous with several names. Different programming languages name them differently like functions, methods, sub-routines, procedures, etc. So when you come across any such terminology, then just imagine about the same concept, which we are going to discuss in this tutorial.
Let's start with a program where we will define two arrays of numbers and then from each array, we will find the biggest number. As we already have seen following are the steps to find out maximum number from a given set of numbers:
1. Get a list of numbers L1, L2, L3....LN 2. Assume L1 is the largest, Set max = L1 3. Take next number Li from the list and do the following 4. If max is less than Li 5. Set max = Li 6. If Li is last number from the list then 7. Print value stored in max and come out 8. Else prepeat same process starting from step 3 Let's translate above program in C programming language:
#include <stdio.h> main() { int set1[5] = {10, 20, 30, 40, 50}; int set2[5] = {101, 201, 301, 401, 501}; int i, max; /* Process first set of numbers available in set1[] */ max = set1[0]; i = 1; while( i < 5 ) { if( max < set1[i] ) { max = set1[i]; } i = i + 1; } printf("Max in first set = %d\n", max ); /* Now process second set of numbers available in set2[] */ max = set2[0]; i = 1; while( i < 5 ) { if( max < set2[i] ) { max = set2[i]; } i = i + 1; } printf("Max in second set = %d\n", max ); } When the above code is compiled and executed, it produces the following result:
Max in first set = 50 Max in second set = 501 If you are clear about the above example, then it will become easy to understand why do we need a function. Here in above example, I took only two sets of numbers set1, and set2 but consider a situation we have 10 or more similar sets of numbers to find out maximum numbers from each set. In such situation, we will have to repeat same processing 10 or more times and ultimately program will become too large with repeated code. To handle such situation, we write our functions where we try to keep source code which will be used again and again in our programming.
Now, let's see how to define a function in C programming language and then subsequent section will explain how to use that function:

Defining a Function:

The general form of a function definition in C programming language is as follows:
return_type function_name( parameter list ) { body of the function return [expression]; } A function definition in C programming language consists of a function header and a function body. Here are all the parts of a function:
  • Return Type: A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.
  • Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature.
  • Parameter List: A parameter is like a placeholder. When a function is invoked, you pass a value as a parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
  • Function Body: The function body contains a collection of statements that define what the function does.

Calling a Function:

While creating a C function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task.
Now, let's write above example with the help of a function:
#include <stdio.h> int getMax( int set[] ) { int i, max; max = set[0]; i = 1; while( i < 5 ) { if( max < set[i] ) { max = set[i]; } i = i + 1; } return max; } main() { int set1[5] = {10, 20, 30, 40, 50}; int set2[5] = {101, 201, 301, 401, 501}; int max; /* Process first set of numbers available in set1[] */ max = getMax(set1); printf("Max in first set = %d\n", max ); /* Now process second set of numbers available in set2[] */ max = getMax(set2); printf("Max in second set = %d\n", max ); } When the above code is compiled and executed, it produces the following result:
Max in first set = 50 Max in second set = 501 For now, its enough for you to know about what are functions and how do they work. If you understood this concept then you can proceed for a detailed tutorial to drill it down further.

Functions in Java

If you are clear about functions in C programming, then its easy to understand them in Java as well. Java programming names them as methods , but rest of the concepts remain more or less same as we discussed in C programming.
Following is the equivalent program written in Java programming language. You can try to execute the following program to see the output:
public class DemoJava { public static void main(String []args) { int[] set1 = {10, 20, 30, 40, 50}; int[] set2 = {101, 201, 301, 401, 501}; int max; /* Process first set of numbers available in set1[] */ max = getMax(set1); System.out.format("Max in first set = %d\n", max ); /* Now process second set of numbers available in set2[] */ max = getMax(set2); System.out.format("Max in second set = %d\n", max ); } public static int getMax( int set[] ) { int i, max; max = set[0]; i = 1; while( i < 5 ) { if( max < set[i] ) { max = set[i]; } i = i + 1; } return max; } } When above program is executed, it produces the following result:
Max in first set = 50 Max in second set = 501

Functions in Python

Once again, if you already understood the concept of functions in C and Java programming, then Python is not much different in defining and calling functions. Following is basic syntax of defining a function in Python:
def function_name( parameter list ): body of the function return [expression] So using this syntax of function in Python, above example can be written as follows:
def getMax( set ): max = set[0] i = 1 while( i < 5 ): if( max < set[i] ): max = set[i] i = i + 1 return max set1 = [10, 20, 30, 40, 50] set2 = [101, 201, 301, 401, 501] # Process first set of numbers available in set1[] max = getMax(set1) print "Max in first set = ", max # Now process second set of numbers available in set2[] max = getMax(set2) print "Max in second set = ", max When the above code is executed, it produces the following result:
Max in first set = 50 Max in second set = 501