Rabu, 17 Desember 2025

listing4.5.cpp Analyzing the Null Terminator in a C-Style String

//listing4.5.cpp
#include <iostream>
using namespace std;

int main()
{
    char sayHello[]={'H','e','l','l','o',' ','W','o','r','l','d','\0'};
    cout << sayHello << endl;
    cout << "Size of array: " << sizeof(sayHello) << endl;

    cout << "Replacing space with null" << endl;
    sayHello[5]='\0';
    cout << sayHello << endl;
    cout << "Size of array: " << sizeof(sayHello) << endl;

    return 0;
}

output:
 
Hello World
Size of array: 12
Replacing space with null
Hello
Size of array: 12

Process returned 0 (0x0)   execution time : 0.408 s
Press any key to continue.

ex1303.c Let Me In

/* ex1303.c */
#include <stdio.h>
#include <string.h>

int main()
{
    char password[]="taco";
    char input[15];
    int match;

    printf("Password: ");
    scanf("%s",input);

    match=strcmp(input,password);
    if(match==0)
        puts("Password accepted");
    else
        puts("Invalid password. Alert the authorities.");

    return(0);
}

output:
 
Password: Pepaya
Invalid password. Alert the authorities.

Process returned 0 (0x0)   execution time : 4.751 s
Press any key to continue.

Selasa, 16 Desember 2025

listing4.4.cpp Creating A Dynamic Array of Integers and Inserting Values Dynamically

//listing4.4.cpp
#include <iostream>
#include <vector>

using namespace std;

int main()
{
        vector<int> dynArray(3); //dynamic array of int

        dynArray[0]=365;
        dynArray[1]=-421;
        dynArray[2]=789;

        cout << "Number of integers in array: " << dynArray.size() << endl;

        cout << "Enter another element to insert" << endl;
        int newValue=0;
        cin >> newValue;
        dynArray.push_back(newValue);

        cout << "Number of integers in array: " << dynArray.size() << endl;
        cout << "Last element in array: ";
        cout << dynArray[dynArray.size()-1] << endl;
        return 0;
}

output:
 
Number of integers in array: 3
Enter another element to insert
2025
Number of integers in array: 4
Last element in array: 2025

Process returned 0 (0x0)   execution time : 19.301 s
Press any key to continue.