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);

Output
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);

Output
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);

Output
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));

Output
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.");
else
    System.out.println("Java Strings are not equal.");
   
if (a.equals(b))
    System.ou.println("Both Java Strings are equal.");
else
    System.out.println("Java String are not equal.");
   
Output
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);

Output

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);

Output
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);

Output
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
}
else
{
// 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" ); 
else 
{ 
do
{ 
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) */ 
}
fclose(fp); 
}

Miyerkules, Disyembre 7, 2011

String.h

▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓
▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒ 
▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓
▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓ 

String.h
--string.h is the header in the C standard library for the C programming language which contains macro definitions, constants, and declarations of functions and types used not only for string handling but also various memory handling functions; the name is thus something of a misnomer.

String copy functions
•strcpy() - copies a string to another
•strncpy() - copies n characters from a string to another

SAMPLE PROGRAM #1 [strcpy()]


#include <string.h>
  #include <stdio.h>
  #include <conio.h>
  int main()
  {
  char s1[100],s2[100];
  strcpy(s1,"hello");
  printf(“%s”,s1);
  getch();
  }


SAMPLE PROGRAM #2 [strncpy()]


#include <string.h>
  #include <stdio.h>
  #include <conio.h>
  int main()
  {
  char s1[100],s2[100];
  strncpy(s1,"hello“,2);
  printf(“%s”,s1);
  getch();
  }

String concatenation
•strcat() - concatenates two strings
•strncat() - concatenates n characters from a string onto another




SAMPLE PROGRAM #3 [strcat()]

  #include <stdio.h>
  #include <string.h>
  int main ()
  {
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str); //printf in string.h
  getch();
  }


String Compare
•strcmp() - compares two strings
•strncmp() - compares n characters of two strings


SAMPLE PROGRAM #4 [strcmp()]

  #include <stdio.h>
  #include <string.h>
  int main()
  {
  char s[10] = "testing", t[10];
  strcpy(t, s);
   if (strcmp(s, t)==0)
  printf("The strings are identical.\n");    else
  printf("The strings are different.\n");
  getch();
  }


Other String Functions
•strchr() - returns the first occurrence of character in a string
•strrchr() - returns the last occurrence of character in a string
•strspn() - returns the length of leading characters in a string that are contained in a specified string
•strcspn() - returns the length of leading characters in a string not contained in a specified string
 

SAMPLE PROGRAM #5 [strchr()]

  #include <stdio.h>
  #include <string.h>
  int main ()
  {
  char str[] = "This is a sample string";
  char * pch;
  printf ("Looking for the 's' character in \"%s\"...\n“,str);
  pch=strchr(str,'s');
  while (pch!=NULL)
  {
  printf ("found at %d\n",pch-str+1);           

  pch=strchr(pch+1,'s');
  }
  getch();
  }


[OUTPUT]
Looking for the 's' character in "This is a sample string"...
found at 4
found at 7
found at 11
found at 18


SAMPLE PROGRAM #6 [strrchr()]


  #include <stdio.h>
  #include <string.h>
  int main ()
  {
  char str[] = "This is a sample string";
  char * pch;
  pch=strrchr(str,'s');
  printf ("Last occurence of 's' found at %d \n",pch-str+1);
  return 0;
  }


[OUTPUT]
Last occurrence of 's' found at 18

CONTINUATION
•strpbrk () - returns first occurrence in a string of any character in another string
•strstr () - returns the first occurrence of one string in another
•strlen()  - returns length of a string
•strtok()  - returns a token from a string delimited by specified characters
•strrev() – reverses all characters except the null terminator.

SAMPLE PROGRAM #7 [strpbrk()]


  #include <stdio.h>
  #include <string.h>
  int main ()
  {
  char str[] = "This is a sample string";
  char key[] = "aeiou";
  char * pch;
  printf ("Vowels in '%s': ",str);
  pch = strpbrk (str, key);
  while (pch != NULL)
  {
  printf ("%c " , *pch);
  pch = strpbrk (pch+1,key);
  }
  printf ("\n");
  return 0;
  }


[OUTPUT]
Vowels in 'This is a sample string': i i a a e i

SAMPLE PROGRAM #8 [strstr ()]


#include <stdio.h>
#include <string.h>
int main ()
{
  char str[] ="This is a simple string";
  char * pch;
  pch = strstr (str,"simple");
  strncpy (pch,"sample",6);
  puts (str);
  return 0;
}


[OUTPUT]
This is a sample string
 

SAMPLE PROGRAM #9 [strlen()]

#include <stdio.h>
#include <string.h>
int main ()
{
  char szInput[256];
  printf ("Enter a sentence: ");
  gets (szInput);
  printf ("The sentence entered is %u characters long.\n",strlen(szInput));
  return 0;
}


[OUTPUT]
Enter sentence: just testing
The sentence entered is 12 characters long.

SAMPLE PROGRAM #10 [strtok()]


#include <stdio.h>
#include <string.h>
int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
  printf ("%s\n",pch);
  pch = strtok (NULL, " ,.-");
   }
  return 0;
}

[OUTPUT]
Splitting string "- This, a sample string." into tokens:
This
a
sample
string

SAMPLE PROGRAM #11 [strtok()]


#include <stdio.h>
#include <string.h>
int main ()
{
  char str[] =“reverse";
  strrev(str);
  puts(str);
  return 0;
}

[OUTPUT]
esrever


▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓
▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒ 
▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓ 
▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒▓▒

Holiday picture Collection


                                                                   AC II art

Huwebes, Oktubre 13, 2011

 
          Write a program that asks the user to enter a number n and display the first n odd numbers. Example: if n=5, the first 5 odd numbers are 1, 3, 5, 7 and 9.


     #include<stdio.h>
     #include<conio.h>
     #define min printf
     #define jee scanf

     int main()
     {
         int m, j;
   
         min("Enter a number: ");
         jee("%d",&j);
   
              for (m=1;m<=j*2;m+=2)
                 {
                 min("%d\n",m);
                 }
         getch ();
         return 0;
     }
                                                                     Sample Output:


                                                                         ▌▌▌Ü ▌▌▌