STRUKTUR DATA
Type Data String
Deklarasi :
C string biasanya di deklasi sebagai array of char, namun perlu
diperhatikan bahwa array of char tidaklah otomatis sebuah
string.
Valid C string harus di ahiri oleh karakter null, yaitu sebuah
karakter ascii bernilai 0 (nol) atau sering ditulis sebagai \0
Char adalah type data built in pada c, sehingga membuat array
of char tdk diperlukan header file, namun untuk memprosesnya
kita membutuhkan fungsi dan utilty, maka untuk
mengoperasikannya kita perlu meng-include header file
<include cstring.h>
Contoh deklarasi sebagai array of char :
char S1[20];
ini adalah deklarasi array of char yang dapat menyimpan string
// string
char S1[20]=hello;
singkat
// deklarasi
char S1[20]=
kosong
// string
C Input String
//--------------------------------------------------------------------------#include <stdio.h>
#include <iostream.h>
#include <conio.h>
// utk getch()
#include <clx.h>
#pragma hdrstop
//--------------------------------------------------------------------------#pragma argsused
int main(int argc, char* argv[])
{
char str[20];
printf("Enter your string : ");
scanf("%[^\t\n]s",str);
// scanf to accept multiword string
printf("String = %s",str);
string
getch();
return 0;}
//---------------------------------------------------------------------------
//--------------------------------------------------------------------#include <stdio.h>
#include <iostream.h>
#include <conio.h>
// utk
getch()
//-------------------------------------------------------------------------#include <stdio.h>
#include <iostream.h>
#include <conio.h>
// utk getch()
#include <clx.h>
#pragma hdrstop
#include <clx.h>
//-------------------------------------------------------------------------#pragma hdrstop
#pragma argsused
//-------------------------------------------------- int main(int argc, char* argv[])
-------------------{
#pragma argsused
char nama[40];
int main(int argc, char* argv[])
{
cout << "nama : ";
char nama[40];
cin >> nama;
printf("nama : ");
cin.getline(nama,40);
printf("nama : %s",nama);
getch();
return 0;
return 0;
}
//--------------------------------------------------
}
//--------------------------------------------------------------------------
Karena S3 adalah pointer maka kita juga dapat mengakses individual charakter
dari stringnya via pointernya, contoh :
cout << *s3 << endl;
// Prints the character 'h', the character pointed to by
s3
cout << *(s3 + 4) << endl; // Prints the character 'o', the fifth character in the
string "Hello
String Length :
You can obtain the length of a C string using the C library function strlen().
This function takes a character pointer that points to a C string as an argument.
It returns an unsigned int, the number of valid characters in the string (not
including the null char).
s2)
s2)
s2)
s2)
s2)
s2)
// Prints the character 'e', the second character in the string "Hello"
The reason this works is a C++ feature called operator overloading. Its actually calls a special
member function named operator[] that has been defined as part of the string class.
The subscript specified inside the brackets is passed as an argument to the member function,
which then returns the character at that position in the string.
The name of a C++ string object is not a pointer and you can not use pointer notation with it
or perform pointer arithmetic on it.
String Length :
You can obtain the length of a C++ string using the string class methods length() or size().
Both of methods return an unsigned int.
the number of valid characters in the string (not including the null character).
Examples :
string s = "Some text";
cout << "String length is " << s.length() << endl; // Length is 9
// Loop through characters of string
for (int i = 0; i < (int) s.size(); i++)
cout << s[i];
cout << endl;
C++
S1 & S2 coba bandingkan dgn operator ts
Screen Capture hasilnya
Examples :
if (s1 > s2) // Compare two C++ strings
if ("cat" == s2) // Compare C string literal and C++ string
if (s3 != cstr) // Compare C++ string and array containing C string
Like subscripting, this works because of operator overloading.
Concatenation :
The operator + may be used to concatenate C++ strings.
C++ strings, C strings, and string literals may all be concatenated together in any order.
The result is a C++ string object that may be assigned to another C++ string object, passed
Latihan
7 : Buat printed,
Program
to a function that takes a C++ string object
as an argument,
etc.
string s1 = "Hello";
string s2 = " good ";
char s3[10] = "friend";
s1 = s1 + ", my " + s2 + s3;
Fungsi2 string
C++
Note : C string yang dibuat dgn cara ini tidak bisa diubah tapi bisa
digunakan untuk ditampilkan atau di copy.