extract substring – C
16 Dec 2011 Leave a Comment
#include <stdio.h>
char *substring(size_t start, size_t stop, const char *src, char *dst, size_t size)
{
int count = stop - start;
if ( count >= --size )
{
count = size;
}
sprintf(dst, "%.*s", count, src + start);
return dst;
}
int main()
{
static const char text[] = "The quick brown fox jumps over the lazy dog.";
char a[10], b[5];
printf("substring = \"%s\"\n", substring(4, 13, text, a, sizeof a));
printf("substring = \"%s\"\n", substring(4, 13, text, b, sizeof b));
return 0;
}
/* my output
substring = "quick bro"
substring = "quic"
*/
http://www.linuxquestions.org/questions/programming-9/extract-substring-from-string-in-c-432620/
C input line argc and argv
16 Dec 2011 Leave a Comment
Sometimes it is useful to pass information into a program when you run it.
Generally, you pass information into the main( ) function via command line arguments.
A command line argument is the information that follows the program’s name on the command line of the operating system.
For example, when you compile a program, you might type something like the following after the command prompt:
at windows
> program_name
at linux
./program_name
where program_name is a command line argument that specifies the name of the program you wish to compile.
There are two special built-in arguments, argv and argc, that are used to receive command line arguments.
The argc parameter holds the number of arguments on the command line and is an integer. It is always at least 1 because the name of the program qualifies as the first argument.
The argv parameter is a pointer to an array of character pointers. Each element in this array points to a command line argument. All command line arguments are strings
any numbers will have to be converted by the program into the proper internal format.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if(argc!=2) {
printf("You forgot to type your name.\n");
exit(1);
}
printf("Hello %s\n", argv[1]);
return 0;
}
next code, write countdown
/* Countdown program. */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, char *argv[])
{
int disp, count;
if(argc<2) {
printf("You must enter the length of the count\n");
printf("on the command line. Try again.\n");
exit(1);
}
if(argc==3 && !strcmp(argv[2], "display")) disp = 1;
else disp = 0;
for(count=atoi(argv[1]); count; --count){
if(disp) printf("%d\n", count);
putchar('\a'); /* this will ring the bell */
}
printf("Done\n");
return 0;
}
POST and GET method WEB PHP
02 Nov 2011 Leave a Comment
in php
in website, method to sending variable is divide two
first GET, if you looking the url bar like http://somedomain.com/index.php?value=given
value=given is the variable which sending with method get, we can read it trough
second POST, you cannot see the value without some technique or tools
PHP know all this method and divide they two with method $_GET and $_POST to handle the value
notice this:
when you send value with method GET you must use $_GET method to get the value, and same with POST
in some case, you have build some function with method GET or POST (but not both) to get value, and forward it to algorithm, if you work the method with structural programming, you design the function can use / work with other module or function or class, sometimes you need to switch method from GET to POST or otherwise
solution:
you build some method to doing two method getting variable,
Algorithm: use first method, check it, if not set, use second method
function notEmpty($str='id', $d){
if(!isset($id)) $id = $_GET[$str];
if(empty($id)) $id = $_POST[$str];
if(empty($id)){
if(empty($d) && !isset($d))
die("empty value $str, stop the operation");
else
return $d;
} else return $id;
}
this is like open global variable, so you must carefully use this, just use if ‘URGENT’
onclick event change url using jQuery
11 Oct 2011 Leave a Comment
in ajax, javascript, jquery
<script type="text/javascript" src="jquery-1.6.4.min.js"></script>
<script type='text/javascript'>
$(document).ready(function() {
$("input:button").click(function() {
location.href = $("#urllist option:selected").val();
});
});
</script>
URL:
<select id='urllist'>
<option value='http://www.google.com'>google</option>
<option value='http://localhost'>localhost</option>
<option
value='http://forum.jquery.com/topic/onclick-event-change-url-using-jquery'
selected>source</option>
</select>
<input type="button" value="Go" />
</select>
Single Series FusionChart XML
03 Oct 2011 Leave a Comment
<?php
include_once('database.php');
include_once('mysqlimproved.php');
include_once('FusionCharts_Plugin.php');
$q = "select KdKat as kode, short as nama from g3n_kategori";
$db = new MysqlImproved_Driver;
$db->connect();
$db->prepare($q);
$db->query();
$ar = $db->datashet();
// constructor
$fc = new FusionCharts('Pie3D', 400, 300, true);
//setting path
$fc->setSWFPath('./FC/');
// setting parameter
$fc->setChartParams("caption=Factory Sales;subCaption=Year 2007;bgColor=ffccff");
// add chart data
foreach($ar as $var=>$val){
$fc->addChartData($val['kode'], "name=".$val['nama'].";color=ff0000;link=http://www.FusionCharts.com");
}
//change header
header('Content-type: text/xml');
// make it out
print $fc->getXML();
?>
contoh script JSON (JQuery Framework) AJAX PHP
19 Sep 2011 Leave a Comment
in ajax, javascript, jquery
ada 3 file yg di perlukan
- view.php
- load.php
- jquery.js
view.php: sebagai html view / melakukan pemanggilan post ajax
<script type="text/javascript" src="jquery-1.6.4.min.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
$('#id_propinsi').change(
function(){
load_combobox_kabkota($(this).val());
});
});
function load_combobox_kabkota(val){
$.post("./load.php", { 'kode' : val },
function(data){
var strbody = '';
for(var i in data){
var nomor = i; nomor++;
strbody += "<option value='"+data[i].id+"'>";
strbody += data[i].kota+"</option>";
}
$('#id_kabkota').html(strbody);
}, "json");
}
</script>
Provinsi:
<select id='id_propinsi' name='div_propinsi'>
<option value='31'>DKI Jakarta</option>
<option value='32'>Jawa Barat</option>
</select>
<br />
Kab/Kota :
<select name='id_kabkota' id='id_kabkota'>
</select>
load.php: menerima nilai post, dan mengirim balik nilai array
<?php
$b = array
(
array('id'=>311, 'kota'=>'Jakarta Barat'),
array('id'=>312, 'kota'=>'Jakarta Timur'),
array('id'=>313, 'kota'=>'Jakarta Selatan'),
array('id'=>314, 'kota'=>'Jakarta Utara'),
array('id'=>315, 'kota'=>'Kep. Seribu')
);
$b = array
(
array('id'=>321, 'kota'=>'Bogor'),
array('id'=>322, 'kota'=>'Sukabumi'),
array('id'=>323, 'kota'=>'Cianjur')
);
$pilih = $_POST['kode'];
if($pilih == '31')
echo json_encode($a);
else
echo json_encode($b);
?>
Dynamic Invocation PHP: is_callable, call_user_func, call_user_func_array
19 Sep 2011 Leave a Comment
in OOP
melakukan pemanggilan dan pe-lambda-an / dynamic invocation kedalam fungsi kelas
contoh penggunaan lambda dari fungsi:
$lambda = rand(0,1) ? function() { echo "Heads!"; } :
function() { echo "Tails!"; };
$lambda(); // <= Dynamic Invocation!
sementara untuk is_callable adalah sebuah methode untuk menentukan apakah sebuah fungsi dapat dipanggil/dideklarasikan atau tidak
function heads() { echo "Heads function!"; }
$function = 'heads';
is_callable($function); // true
$lambda = function() { echo "Tails function!"; }
is_callable($lambda); // true
$asalAja = "loramIpsumSitAmet";
is_callable($asalAja); // false
sementara untuk fungsi didalam kelas kita tidak dapaat menggunakan is_callable
class Objects {
function methods($who, $what) {
echo "$who are Great $what!";
}
}
$callableArray = array(new Objects, 'methods');
if(is_callable($callableArray)) {
call_user_func($callableArray, "You", "Success");
call_user_func_array($callableArray, array("You", "Success"));
}
perintah call_user_func dan call_user_func_array berhasil dijalankan
membuat cd / dvd to iso ubuntu 10.10
13 Sep 2011 Leave a Comment
in Tips n Trik, ubuntu
membuat file iso dari cd atau dvd dengan menggunakan ubuntu
1. masukan cd/dvd ke drive (cd/dvd) rom
2. buka system monitor untuk melihat drive cd/dvd (System -> Administration -> System Monitor)
3. lihat drive cd room
4. berikan perintah, cat /dev/sr0 > nama.iso
Inheritance dan Interface OOP (PHP)
06 Sep 2011 Leave a Comment
Perbedaan Interface dan Inheritance (PHP programing)
saat kita mendeklarasikan beberapa class anggaplah kelas propinsi, kotakabupaten, dan kecamatan
setiap kelas yang kita sebutkan diatas memiliki beberapa fungsi yang harus dideklarasikan
seperti fungsi input, update, delete
tetapi mungkin saja class kotakabupaten memiliki fungsi lain yaitu fungsi view_kabupaten_propinsi
maka si fungsi input, update, delete kita daftarkan dalam interface dan fungsi view_kabupaten_propinsi kita deklarasikan dalam class kotakabupaten
sementara setiap class yang ingin dibuat (propinsi, kotakabupaten, dan kecamatan) memiliki sebuah fungsi __construct yang sama untuk semua kelas ini maka kita dapat membuat menjadi seperti ini
# interface
class Regional {
function input();
function update();
function delete();
}
# Inheritance
class Daerah {
function __construct(){
// ... code construct declare here
}
}
# class using inheritance and interface
class Propinsi extends Daerah interface Regional {
function input(){
// declare code input for propinsi
}
function update(){
// declare code update for propinsi
}
function delete(){
// declare code delete for propinsi
}
}
$p = new Propinsi();
class propinsi akan menggunakan fungsi __construct yang diturunkan dari class Daerah
sementara jika kelas kabupaten kota memiliki sebuah fungsi berbeda dari class yang lain yaitu view_kabupaten_propinsi kita dapat membuat nya menjadi
class Kabkota extends Daerah interface Regional {
function input(){
// declare code input for kabkota
}
function update(){
// declare code update for kabkota
}
function delete(){
// declare code delete for kabkota
}
// khusus untuk class Kabkota
function view_kabupaten_propinsi(){
// declare here
}
}
dengan cara seperti ini kita telah mengurangi pengulangan penulisan fungsi yang identik
dan menjamin sebuah interface yang sama pada kelas2 yang dibuat
Grammar Cheat: Simple Present & Present Progressive
23 Aug 2011 Leave a Comment







