Lunes, Marso 5, 2012

Java Strings

Java String Array

String[] str_array = new String[2];

str_array[0] = "Java String";
str_array[1] = "Java String Array";

System.out.println("Index 0 : " +str_array);
System.out.println("Index 1 : " +str_array);

Index 0 : Java String
Index 1 : Java String Array


Java String - Concatenation

String a = "Java String1";
String b = "Java String2";

System.out.println("Concatenating String: " +a+b);

Concatenating String: Java String1JavaString2


Java: Convert String to int

String str_int = "10";
int a = Integer.parseInt(str_int);
//printing a which is integer
System.out.println("Java String to int: " +a);

Java String to int: 10


Java Split String

String str_a = "Java String";
//Java String Length
int le = str_a.length();
System.out.println("Length of string is: " +len);
//start index 0
System.out.println("Character at 5: " +str_a.charAt(5));
System.out.println("Characters from 5 till end: " +str_a.substring(5, len));

Length of string is: 11
Character at 5: S
Character from 5 till end: String


Java String Comparison

String a = new String("Java String");
String b = new String("Java String");

if (a == b)
    System.out.println("Both Java Strings are equal.");
    System.out.println("Java Strings are not equal.");
if (a.equals(b))
    System.ou.println("Both Java Strings are equal.");
    System.out.println("Java String are not equal.");
Java String are not equal.
Both Java Strings are equal.


Java String Replace

String str_orginal = "A Java String";
String str_replaced = str_original.replace('a','b');
System.out.println("Original String = " +str_original);
System.out.println("Replaced String = " +str_replaced);


Original String = A Java String
Replaced String = A Jbvb String


Java String Upper/Lower Cases

String str_orig = "Java String";
System.out.println("Original String: " +str_orig);
String str_low = str_orig.toLowerCase();
System.out.println("Lower case String: " +str_low);
String str_upper = str_orig.UpperCase();
System.out.println("Upper case String: " +str_upper);

Original String: Java String
Lower case String: java string
Upper case String: JAVA STRING


Java String Trim

String str_a = "Java String";
System.out.println("String is: " +str_a+ " of length: " +str_length());
System.out.println("Trimmed String is: " +str_a.trim()+ " of length: " +str_a.length);

String is: Java String of length: 16
Trimmed String is: Java String of length: 12


Huwebes, Pebrero 2, 2012

File Handling In C

~~~~~~~~~~ Overview ~~~~~~~~~~

Part of any useful computer program is often going to be manipulation of external data sources. The file is the basic unit of storage for many operating systems, from Unix to Mac. Any C development environment on these platforms will include functions allowing the programmer to:

Open & Close files;
Read from & Write to files;
Delete files.

Commands for all of the above are found in the stdio.h header file.

~~~~~~~~~~ Opening and Closing Files ~~~~~~~~~~

The value returned is a handle to a file, defined in stdio.h, as FILE *. A null pointer is returned and can be tested for in case the call fails. So, the following is a good test for presence of a file:

FILE * hFile;
hFile = fopen( filename, "r");
if (hFile == NULL)
// Error, file not found
// Process & close file//
fclose (hFile);

~~~~~~~~~~ File Modes ~~~~~~~~~~

The mode value in the above example is set to 'r', indicating that we want to read from the file. Other possible values are:
w - write to the file, overwriting existing data
a - append to an existing file
r+ - read and write to a file
Using 'r+' means that all writing will take place at the end of the file, and that reading is sequential. 

The fclose function closes a valid file handle.

~~~~~~~~~~ Reading a file ~~~~~~~~~~

When an r is used, the file is opened for reading
Using the r indicates that the file is assumed to be a text file.

/* Program to display the contents of a file on screen */ 
#include <stdio.h> 
void main() 
FILE *fp; 
char c; 
fp = fopen ( “sample.txt" , "r" ); 
c = getc (fp) ; 
while (c != EOF) 
putchar (c); 
c = getc (fp); 
fclose (fp); 
~~~~~~~~~~ Writing a file ~~~~~~~~~~

When a file is opened for writing, it will be created if it does not already exist and it will be reset if it does, resulting in the deletion of any data already there

#include <stdio.h> 
int main() 
FILE *fp; 
fp = fopen("file.txt","w");          /*Create a file and add text*/ 
fprintf(fp,"%s","This is just an example :)");          /*writes data to the file*/ 
fclose(fp);          /*done!*/ 
return 0; 
~~~~~~~~~~ Appending a file ~~~~~~~~~

When a file is opened for appending, it will be created if it does not already exist and it will be initially empty
If it does exist, the data input point will be positioned at the end of the present data so that any new data will be added to any data that already exists in the file.
#include <stdio.h> 
int main() 
FILE *fp 
fp = fopen ( “sample.txt" , "a" );
fprintf (fp,"%s","This is just an example :)");          /*append some text*/
fclose (fp); 
return 0; 
~~~~~~~~~~ Reading from a File ~~~~~~~~~~

#include <stdio.h> 
main( ) 
FILE *fp; char c; 
fp = fopen( “SAMPLE.TXT", "r" ); 
if (fp == NULL) 
printf ( "File doesn't exist\n" ); 
c = getc (fp);          /* get one character from the file */ 
putchar (c);           /* display it on the monitor */ 
while (c != EOF); /* repeat until EOF (end of file) */