How to convert a String to an Array ?


     At times, we need to convert a string in to an array in order to use it in our program so how do we do that. Below I have a JAVA program that will help you to convert any String to an Array.

     First we declare a variable of String data-type. Then we ask the user to enter the string that is to be converted to Array. To read the input by the user we use Scanner. Now Java provides a predefined function to convert a data-type to Character-Array, which is toCharArray();

     This function in particular converts the data provided by the user to CharArray. Later we display this converted data properly in an array format. For that we use ‘for loop’ and declare an integer initializing it to zero, which has to be strictly less than the length of our string. By using post increment we can display how each character gets stored one by one in the array.


Here is the code for it:

        char a[]=s.toCharArray();
          for (int i=0;i<s.length();i++){
             System.out.println("Data at ["+i+"]= "+a[i]);}

       Although you should be careful with whitespaces as they terminate the string and the compiler neglects whatever that comes after space when it encounters one.


  • To download the complete program click here:

Download String to Array project

To convert an Array to String checkout his program I made recently How to Convert an Array to a String.


Periodic Table C Program.


Hey, I am happy to upload my first program on the internet. This program is written in C language. It is a program to find Atomic numbers of certain elements. Read the ‘Readme’ file to know how I made this program.

Download the source code and the text file here:-

Download periodic table project

For the execution of this program, all these files are required and should be saved in the same directory. If you have downloaded the source code open the bcpt.c file with a text editor. I have first declared three variables along with their appropriate data types , for the storage of all the elements of periodictable.txt, b , to store the element specified by the user, and a , to store the matched string after scanning the text file.

By specifying file pointer FILE *fp the text file periodictable.txt is opened in read mode.


I have also put up an ‘if’ check to notify the user if the text file does not exist in the main directory. If the file has been removed the program exits the execution.

When the file is open it gets scanned line by line until the End of file isn’t encountered and all of its data gets stored in the variable e. Meanwhile, we ask the user to enter an element and it gets scanned and the stored in variable b.


The string compare function strcmp() is used in order to compare the strings stored in variable e and variable b. If they match that line is returned to the main.

The while loop specifically helps strcmp() in comparing each and every line one at a time until the end of the file and of the comparison matches the index of that particular file is returned and stored in another variable b. Later this variable is displayed on to the terminal or cmd with printf() function.

If the variable doesn’t exist in the text file then the else part is executed and you can printf() that the element entered by the user does not exist. I have used a flag found for stating the true or false as per the existence of the file. The strcmpi() lets the user a freedom to enter the element in upper case and lower case.


       if(strcmpi(b,e)==0){     /*Compare string one by one*/
            printf("The atomic number of %s is %d .\n",b,a);
            printf("The input element does not exist.”);

This program can be used for lots of other things like finding the values of certain physical constants like gravitational constant ‘G’. Also, you can access lots of other kinds of data with this program, you just have to edit the text file and the source file according to your needs.
Execute on Windows :- Open Command Prompt and (Change to the bcpt.c file’s directory), type :

> gcc bcpt.c
> a/.out

Execute on Linux :- Open Terminal and type :

$ gcc bcpt.c
$ ./a.out



The Modern Periodic table is referred from Wikipedia. If you find any bugs or have any suggestion to this program comment below and let me know. Happy programming !

Time Difference Calculator C Program.


          Using Call by reference I have made a C program to calculate time difference between start time and end time. First we declare three integers in a structure ‘sec, min, hrs’ to store the timings in hours:minutes:seconds format. Then we declare a function Difference to which we are going to call later in the program.

   void Difference (struct TIME t1, struct TIME t2, struct TIME *diff);

  Then variables ‘t1,t2,diff’ are to be declared to store the values of start time and end time. First we will ask the user to enter the start time and then store it in t1 variables. Then we ask the user the end time and store it in variable t2. When we have got both the times we then call the function Difference.

   Difference (t1,t2,&diff);

   In difference function we use If…else condition to check which one of the time is greater so as to subtract smaller value from greater one by one. The value of hours is checked to find the greatest amongst it and then subtracted. Similarly rest of the time difference is calculated and the difference is displayed on the command prompt.

Here is the code of Difference function:

   void Difference (struct TIME t1, struct TIME t2, struct TIME *differ){
      else differ->hrs=t1.hrs-t2.hrs;

      else differ->min=t1.min-t2.min;

      if (t2.sec>t1.sec){
      else differ->sec=t1.sec-t2.sec;}



Download the Source Code here:-

Download periodic table project