Category Archives: Programming

compiling-executing-c-program

how to compile and run C/C++ program on ubuntu 11.04

C/C++ is one of the most popular programming language, mostly used in system programming (system software development e.g Most of the modern operating systems are programmed (primarily) either in C language or C++). C is a High Level language as compared to Assembly program, so it comes in the middle of Assembly and Java like High Level Language… That’s the beauty of C – it’s very easy to write program in C as compared to Assembly programming and fast execution time as compared to Java.

By default, Ubuntu 11.04 (code name:=Natty Narwhal) or any other Linux Based Distribution like Fedora, Linux Mint(10 or 9), Debian etc.. are very good for programming purposes(e.g C, C++, Java, PHP, Perl, Scripting, Ruby etc.). A lot of free and open source tools like IDE, debuggers are available which will makes the programming a lot Funky, with Ubuntu. In this post you will learn how to compile a C/C++ program on Ubuntu 11.04, from command line, you can also use some IDE like Eclipse, NetBeans if you want (but it’s not recommended for beginners). Ubuntu 11.04 comes with gcc(GNU Compiler Collection), installed by default which is one of the most widely used modern C compiler.

Compiling and Running C program on Ubuntu 11.04

#1 : Write a program, better use gedit (The Default Text editor). Right click on Desktop and create a new file – ‘fun.c’ and copy the code, given below and save it (CTRL+S). (C programs have the extension .c while C++ programs, have .cpp, although it’s a different fact that Linux doesn’t recognize file based on their extension but some applications may do that and it also avoid confusion, so better use proper extension)

#include<stdio.h>
void main()
{
/* Don't take it seriously, it's just for Fun! */
printf("Have Fun!\n");
}

#2 : Now you compile the program using GCC compiler (I think it’s installed by default in most versions in Ubuntu, but if it’s not the case then install it by typing – sudo apt-get install gcc at terminal). Open the Terminal (CTRL+ALT+T) and type the command (First move on to the directory where your file is located, I assume you have created the file on Desktop, If you are an absolute beginner – Learn Some basic Linux Commands).

cd Desktop
gcc fun.c -o fun1

The -o option (specifies the Output File Name) in the following command is optional, but it’s a good practice, because if you won’t specify that – then a default a.out file will be created (which will eventually overwrite older a.out file in that directory).

#3 : Execute/Run the program. Type  –

./fun1

#4 : That’s all. Here is one snapshot of output terminal –

compiling-executing-c-programCompile and Run C++ program

To compile C++ program, you may need to install g++.

Installing g++ compiler on Ubuntu 11.04

sudo apt-get install g++

Then all the remaining procedure is almost same. Just replace gcc by g++ from the above tutorial and instead of a basic/simple c program, use C++ program.

g++ file_name.cpp -o fun2
./fun2
netbeans-snapshot

how to install NetBeans IDE on Ubuntu 11.04

NetBeans is one of the most popular Integrated Development Environment ( IDE , which provides a set of tools and features to speed up the development process, so helpful in boosting productivity of programmers). Netbeans is a free and open source Application, available for all common platforms – Linux Distors like Ubuntu 11.04 (Natty Narwhal), Fedora, Linux Mint; Windows and Mac OS X. Netbeans 7.0 (Latest Stable Version) released a week ago. Netbeans is very popular among Java EE developers, because of its features and simplicity. This post will explain how to install netbeans on a freshly installed ubuntu 11.04, from USC (Ubuntu Software Center) or shell, you can also use Synaptic Package Manager.

You can also Try Eclipse IDE on Ubuntu 11.04, if you want, as Eclipse is another very popular open source IDE, but Netbeans is recommended, for beginner programmers because it’s very simple, lightweight and easy to start. A lot of free plugins are available to extend the features (support for Ruby and Rails, C/C++, PHP etc) of Netbeans IDE. I , personally don’t recommend IDE to beginner programmers, because of the fact that – ‘IDE will hide a lot of things, by creating a layer of abstraction, that we should now’; when you are experienced enough to understand the underlying details, then go for an IDE like NetBeans or Eclipse. Nebeans got 4.5 out of 5.0 in software center rating.

netbeans-snapshot

Features of NetBeans at a Glance

  • good for java (EE features supported) programming
  • Glashfish Server 3.1 is supported
  • faster deployment with Weblogic
  • it’s very simple to use
  • all other basic features (e.g syntax coloring, Auto-completion) that you expect from an IDE

Installing NetBeans on Ubuntu 11.04

#1 : Open Ubuntu Software Center and search for ‘netbeans’.

installing-netbeans-on-ubuntu-11-04

#2 : Click on install button to start the installation procedure and wait for the installation to complete.

#3 : That’s all..Enjoy coding with Netbeans.

Alternative Way – installing NetBeans from Command Line

Open terminal and type the command, followed by your login password –

sudo apt-get update
sudo apt-get install netbeans
installing eclipse ide on ubuntu 11.04

how to install Eclipse IDE on Ubuntu 11.04

Nowadays, IDE i.e Integrated Development Environment is used by most of the Programmers and software developers, as it provides sophisticated software development environment; features like – auto code completion, easy testing and debugging, syntax coloring, framework integration etc. So IDE creates a layer of abstraction, that is certainly helpful for production environment, but may not be good for novice programmers (due to abstraction layer created by IDE, they might lack some background concepts).

There are a number of IDEs available in the Market, but few open source IDE like Eclipse and NetBeans are the most popular. Eclipse IDE is a multi-language software development environment, written in java and available for all platforms like Linux based OS, e.g Ubuntu 11.04 (Natty Narwhal), Debian, Linux Mint, etc, Windows and Mac OS X. Eclipse is a free and open source software (Available under Eclipse Public License, very close to GPL), developed by FOSS community.

Eclipse is the most popular IDE among the Java developers. Eclipse architecture is very flexible and modular, so eclipse is well suited not only for Java programming (with JDT (Java Development Tools) ), but also for C/C++, with CDT (C/C++ Development Tools) components. Most of the frameworks and environments are well suitable with Eclipse; functionality can be easily added with plugins. On Ubuntu 11.04, you can use synaptic package manager or Ubuntu software center or shell to install eclipse in few clicks.

Installing Eclipse IDE on Ubuntu 11.04

Install Eclipse IDE from Ubuntu Software

#1 : Go to main menu (Top Left Corner) and search for ‘ubuntu software center’ or just open it from left econ bar.

#2 : Now, search for ‘eclipse’ in quick search bar.

installing eclipse ide on ubuntu 11.04

#3 : Click on more info (if you want to know more(like Ratings, reviews) before installing, generally recommended), then click on install to begin (You may have to enter your login password), wait until the installation is complete.

#4 : That’s all..you can access it from the main menu (just type ‘eclipse’).

Download and install

Get eclipse from its official website and extract it to any directory (say ‘home’) and make it executable using chmod command, add a path variable (to access directly) if required.

Download Eclipse IDE

Then extract it to the directory ‘/home/your_username’ and make the eclipse.sh file executable using the command-

sudo chmod +x eclipse.sh

Eclipse IDE on Ubuntu 11.04

installing-mysql-on-ubuntu-11.04

How to install MySQL on Ubuntu 11.04

MySQL is the most popular database, mostly used in developing database driven websites or in other words web Applications (Web Apps or Apps). it’s Free and open source. MySQL is a Relational Database Management System (RDBMS), that runs like a server and serves the requests (usually CRUD (Create, Retrieve, Update, Delete) operations) from the clients.

On Ubuntu 11.04 (Alias : Natty Narwhal) , if you want to use any database server, then of course MySQL is the best choice. This post will guide you in installing MySQL server/client (current version : 5.1) from Synaptic Package Manager or Command Line. In last I’ll also show you, how to connect to the MySQL database at the Terminal/shell, although you can also install use graphical tool to manage MySQL database – e.g MySQL Query Browser, MySQL Administrator or phpMyAdmin. MySQL is very popular in the combination of : LAMP – Linux, Apache, MySQL and PHP.

Installing MySQL Server on Ubuntu 11.04

From Synaptic Package Manager

#1 : Open Synaptic Package Manager and search for ‘mysql’. Then select (check the box) the mysql server 5.1 to install. If you want to install some other packages, then click on yes, if it asks for dependencies.

installing-mysql-on-ubuntu-11.04

#2 : Then click on Apply and wait for the installation to complete.

#3 : In the , middle of the installation you will be prompted to Enter the MySQL ROOT Password. Enter a good password for that, but remember!, you need this password to manage the MySQL databases and users, so don’t forget!!

mysql root password

#4 : Relax, until the installation is complete.

From Terminal

sudo apt-get install mysql-server

Now you can connect to MySQL server using the command (In the example the Server Address : localhost, Username : root) –

mysql -h localhost -u root -p

Then Enter your Password (ROOT Password, that you entered in step #3) to connect to the server running at localhost.

Connecting to MySQL server

string reverse in java

Java is one of the most popular programming language used today in developing real world applications (specifically in dynamic and sophisticated web Applications development). Java is strongly typed,object oriented, and multi-platform(even platform independent in general) programming languages. Learning curve with Java is very steep, in beginning ,but it’s very easy when you go ahead, so just relax and move on, solve some problems and write code, That’s the only way you can learn any programming language.

String is considered as an object data type in java. String is immutable (it means you can create and destroy strings but it’s content(characters) can’t can not be altered ) in Java. In this post you will learn, how to reverse a given string in Java. There are lot of ways to reverse a string in Java – the simplest and easy way is to use library methods/functions; but if you are a beginner then better try to do it in different ways because in that way you will learn more.

How to Reverse a String in Java without Using StringBuffer

Here is a program(save it as StringReverse.java and compiler=>execute) to get the reverse string – The code is very simple and comments are written, so no explanation is required,if you have any problem or question or you have any better solution then reply through comment.

import java.util.*;

public class StringReverse {
public static void main(String []args) {
//Get the string to be reversed
System.out.println("Enter the String to reverse");
Scanner scan=new Scanner(System.in);
String str=scan.nextLine();
// Test  System.out.println("The Given String was :"+str);
int stringLength = str.length();
// Test  System.out.println("The Length of the String is : "+stringLength);
char rawData[] =new char[50];
for(int k=0;k<50;k++)
rawData[k]=0;
// Using a for loop to iterate over the characters in string
for(int i=0,j=stringLength-1;i<stringLength;i++,j--) {
rawData[i]=str.charAt(j);
}
String reversedString=new String(rawData);
reversedString=reversedString.substring(0,stringLength);
System.out.println("The Reversed String is : "+reversedString);
//int sLength = reversedString.length();
//System.out.println("The Length of the String is : "+sLength);
}
}

If you are looking for the easiest method to reverse a string in Java, then use StringBuffer class and reverse() method, here is the code for that –

import java.util.*;
public class ReverseTheString {
public static void main(String []args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String");
String str=sc.nextLine();
str = new StringBuffer(str).reverse().toString();
System.out.println("Reversed String : "+str);
}
}