Posts

Showing posts with the label tutorial

Bubble sort in Python Language

Image
Bubble sort is the simplest Sorting algorithm available. It works by repeatedly swapping the adjacent or neighbor element if they are in the wrong order. Working Actually in the first-pass bubble sort sets the largest element to its position, in the second pass algorithm sets the second largest element this continues till the list or array is not finally sorted. Here is the example image. To work with the algorithm you first have to understand what is swapping and how it is done. What is Swapping ?? Swapping is a technique to interchange the content of the two variables, using another variable called a temporary variable. In python programming language swapping works in two ways, both of them are shown below. For the program in Python arr = [64, 34, 25, 12, 22, 11, 90] n = len(arr) for i in range(n): # Last i elements are already in place for j in range(0, n-i-1): # traverse the array from 0 to n-i-1 # Swap if the element found is greater # than the next element if

Bubble Sort in C language

Image
Bubble sort is the simplest Sorting algorithm available. It works by repeatedly swapping the adjacent or neighbor element if they are in the wrong order. Working Actually in first pass bubble sort sets the largest element to its position, in the second pass algorithm sets the second largest element this continues till the list or array is not finally sorted. Here is the example image. To work with the algorithm you first have to understand what is swapping and how it is done. What is Swapping ?? Swapping is the technique to interchange the content of the two variables, using another variable called temporary variable. For the program in C #include <stdio.h> void bubbleSort(int s[],int size){ int c,swap,d; for (c = 0 ; c < size - 1; c++) { for (d = 0 ; d < size - c - 1; d++) { if (s[d] > s[d+1]) /* For decreasing order use < */ { swap = s[d]; s[d] = s[d+1]; s[d+1] = swap

File Handling in C (Reading a file line by line)

Image
In this tutorial, we are working with the files in the C. File handling in any language requires Three main steps to follow Open the file in Read/Write mode. Do various operation that you want to do. Close the file. We are using fopen(file stream). there is some modes to open a file. Read mode  opens file into the read mode if file not found returns -1. Write mode opens file into the write mode if the file does not present creates a new file if there is previous data it will be overwritten. Append mode opens the file in the append mode and append data to it Without wasting much time lets start working. Create a New C file of whatever name you want mine is files.c. create the basic schema of C language. #include <stdio.h> int main(int argc, char const *argv[]) { /* code */ return 0; } Read From the file Create a FILE pointer to point a stream and open a file into the read mode using "r". you may need to include the string.h to your headers. the data of the file is #in

Basics of Pointers In C

Image
Pointers in C A pointer is a variable which contains the address of the variable specified. for example:  int x = 100; int *data; data = &x; this variable data contains the address of the block in the memory which contains the value 100. * specifies that this is the pointer variable. & gives the address. that is the what I am doing here assigning the address of the integer variable x to the pointer variable data. to get the data from the pointers we have to use the * sign. for the previous example to get the data of the memory block of the printf("%d\n",*data );  this should print the output like the image. Further digging into deep for the pointers. we can use them for the array. if we change content in the memory block pointed by the data, then this change should reflect the in the content of the x. Because variable x has the content of that memory block. (this is the great advantage of the using the pointers) we can read, change anything without copying the data fr

How to Get the length of an Array in C

Image
Array in C an array is a collection of a variable of a fixed number of the same data type in a sequential memory. an array can be declared in various ways. one of them is given below int x[] = {200,300,522,655}; char y[] = {'x','s','r'}; int n[10],j; // first declaring the array // then assigning the value. for ( j = 0; j < 10; j++ ) { n[ j ] = j + 100; } indexing in C array starts from the 0. so if you want to access the 4th element then you have to use x[3]. that's enough intro about the array. Getting Length of the Array in C to get the length of the array first we divide the size of the full array by the size of the one element of the array. Please note there is another way of getting the length of the char array (you can use strlen). you can see this in the given program. #include <stdio.h> int main(){ int x[] = {200,300,522,655}; char y[] = {'x','s','r'}; // determining the size of the array // getting the

C/C++ program to check the Palindrome string.

Image
Palindrome string: A string or word which are same from the start and end means word or program which look like same as original and reversed. To do that we have to first reverse the string. Check out our post on the String reverse in C/C++. To write the program of the Palindrome steps used are. Get the input from the user. convert the string to the Lower case to avoid any case sensitivity. apply the logic of the reverse the string. compare the reversed and original string, if same then the string is a palindrome. if different string is not a palindrome. print the result to the user. without wasting much time lets start. First of all the C schema. #include <stdio.h> #include <string.h> #include <ctype.h> int main(int argc, char const *argv[]) { return 0; } ctype.h is used to get the advantage of the function tolower() later in the program. Now take the input from the user. char name[150],rev[150] ; int l=0; printf("Enter a string to check palindrome\n");

C/C++ program to Reverse a String

Image
String In C language string is an Array of the Char variable. to write a program of the string reverse we have to break this program into steps as always. Take input from the user. Reverse the string using the for loop print the final answer to the user. First of All, create a schema for the program. #include <stdio.h> #include <string.h> int main(int argc, char const *argv[]) { return 0; } Now write some code for the Taking input from the user. scanf is fine here but keep in mind it can take input up to first space. char name[150],rev[150] ; int l=0; printf("Enter a string to reverse\n"); // taking input from the user // string should not have the space in between scanf("%s",&name); This code will take a string from the user, now implement the main logic. what we are doing here is that first, we are counting the length of the string and then storing the last variable into the first element of another variable. l = strlen(name); // main logic

How to Use For Each loop in JavaScript

Image
For Each Loop In JavaScript Hello Guys, Having trouble in understanding the concept of the for each loop in javascript. or having trouble finding the correct source code. Then don't worry this post is for you. What is For Each Loop: this is the loop which automatically iterates through all the elements of an array, it helps in the working with the dynamic array. Let's start working. In javascript for-each loop works in a different way. Here we have to create a function which will be passed to the foreach function of the array. Enough discussion. Start working. First, create an array of names of the actor. var heros = ["Tony","Captain","Batman","SuperMan"] Now create the function with the logic that has to be passed in the for each loop. function myfunc(item){ console.log(item + " is a very good superhero"); }  Now call the function in the foreach function of array // running the foreach loop 4 times heros.forEach(myfunc); You c

XML parsing in Python3 (working with ElementTree)

Image
XML Parsing in Python3 XML : XML stands for the eXtensible Markup Language. It was designed for data transport and data storage. It was designed for both Human and machine readable. That's enough for the introduction. Let's start working. First of all the XML file that we are working on. <messages>   <note id="501">     <to>Tove</to>     <from>Jani</from>     <heading>Reminder</heading>     <body>This is code in cafe</body>   </note>   <note id="502">     <to>Jani</to>     <from>Tove</from>     <heading>Re: Reminder</heading>     <body>I will not</body>   </note> </messages> We are using this file as an example XML file, name this file as the "example.xml". Let's start working with the python3. to Parse the XML by the python first, we have to import a module named  xml.etree.ElementTree import xml.etree.ElementTr

String Splitting in C++ (using find and substr)

Image
String Splitting in C++ Hello guys, you all have known the string splitting. Basically, string splitting is nothing but dividing the string based on a Delimiter or special character.  For example, example@gmail.com by the delimiter looks like [example,gmail.com]   use of string splitting is very broad as we can use this to tokenize thing in the web crawlers, data analysis in the data to extract useful things etc. without wasting much time let's start splitting. First of all create a schema for the c++ file. #include <iostream> #include <string> using namespace std; int main() { return 0; } Now let's take a string that is to be split. I took "codeincafeinweb". take a delimiter, for example, I took "in". and one more string variable for tokens. string.find() method returns the starting position of the delimiter, if it founds nothing then it will return -1. That is why we are using the while with terminating condition of -1. string name = "

PHP Connecting to MYSQL Database (creating the simple search box with database)

Image
PHP Connecting to Database In this tutorial, we are working with PHP and MySQL database. We are going to create a simple PHP page that will search for the name entered by the user in the database. NOTE: Full code is in the last of the post.  In order to work with the PHP and MySQL database, you should have installed both in your machine.  check my post for installing the PHP into the Machine. I hope you have installed PHP and MySQL. without wasting much time let's dive to create the page. First, create a file named searchpage.php into your local server directory.  local server directory is a specific folder, which can be accessed by the web server locally or globally. Create the HTML schema which is  And create a form in the body tag. you can try doing this your own. In the form create one input field with the name attribute "name" and create one submit button using the input tag with the name of "submit" and Value of "Search" . now HTML work

Java Console Games Tutorial (number guessing by human)

Image
Java Console Gaming Welcome to the tutorial, this tutorial is all about the small console game which is small to play and write. suppose this program as the practice program, and try doing this on your own, this is highly recommended. if you are stuck somewhere or having any trouble you are free to look here on the page. Features and Rule of the game: Computer asks for a number in variable max number. Computer randomly generates a number and put this number as the guessed number. The computer asks the user to give a number between 0 to max number and reply back with the suitable answer like whether the entered number is lower or higher than the guessed number. you should also assign a variable for the max guess allowed which are 6 per game. When the user wins or lost the game a check for play again will be asked. The game is broken in two files GuessingGame.java and GuessingGameTester.java. GuessingGame.java holds mostly main logic for the game. And GuessingGameTester.java contains the