# cstdlib

***

**1. Generate Random Numbers:**

```cpp
#include <cstdlib>

int main() {
  // Generate a random number between 0 and 99
  int randomNumber = rand() % 100;
  return 0;
}
```

**2. Convert String to Integer:**

```cpp
#include <cstdlib>

int main() {
  // Convert the string "123" to an integer
  int number = atoi("123");
  return 0;
}
```

**3. Convert Integer to String:**

```cpp
#include <cstdlib>

int main() {
  // Convert the integer 123 to a string
  char buffer[100];
  sprintf(buffer, "%d", 123);
  return 0;
}
```

**4. Allocate Memory:**

```cpp
#include <cstdlib>

int main() {
  // Allocate 100 bytes of memory
  int *array = (int *)malloc(100 * sizeof(int));
  return 0;
}
```

**5. Free Allocated Memory:**

```cpp
#include <cstdlib>

int main() {
  // Free the memory allocated in the previous example
  free(array);
  return 0;
}
```

**6. Exit Program:**

```cpp
#include <cstdlib>

int main() {
  // Exit the program with exit code 0
  exit(0);
  return 0; // The return statement will not be executed
}
```

**7. Get Current Process ID:**

```cpp
#include <cstdlib>

int main() {
  // Get the current process ID (PID)
  pid_t pid = getpid();
  return 0;
}
```

**8. Get Environment Variable:**

```cpp
#include <cstdlib>

int main() {
  // Get the value of the environment variable "HOME"
  char *homeDir = getenv("HOME");
  return 0;
}
```

**9. Sort Array:**

```cpp
#include <cstdlib>
#include <algorithm>

int main() {
  int array[] = {5, 2, 4, 1, 3};
  // Sort the array in ascending order
  std::sort(array, array + 5);
  return 0;
}
```

**10. Find Minimum/Maximum Value:**

```cpp
#include <cstdlib>

int main() {
  int array[] = {5, 2, 4, 1, 3};
  // Find the minimum value in the array
  int min = *std::min_element(array, array + 5);
  // Find the maximum value in the array
  int max = *std::max_element(array, array + 5);
  return 0;
}
```

**11. Calculate Power:**

```cpp
#include <cstdlib>
#include <cmath>

int main() {
  // Calculate the power of 2 raised to 5
  double result = pow(2, 5);
  return 0;
}
```

**12. Calculate Logarithm:**

```cpp
#include <cstdlib>
#include <cmath>

int main() {
  // Calculate the logarithm of 100 to the base 2
  double result = log2(100);
  return 0;
}
```

**13. Generate Null-Terminated String:**

```cpp
#include <cstdlib>
#include <cstring>

int main() {
  // Create a null-terminated string from the character array "Hello"
  char *hello = strdup("Hello");
  return 0;
}
```

**14. Compare Strings:**

```cpp
#include <cstdlib>
#include <cstring>

int main() {
  // Compare two strings and return 0 if they are equal, 1 if the first is greater, and -1 if the second is greater.
  int result = strcmp("Hello", "World");
  return 0;
}
```

**15. Copy String:**

```cpp
#include <cstdlib>
#include <cstring>

int main() {
  // Copy one string to another using strcpy
  strcpy(destination, "Hello World");
  return 0;
}
```

**16. Get String Length:**

```cpp
#include <cstdlib>
#include <cstring>

int main() {
  // Get the length of a string
  size_t length = strlen("Hello World");
  return 0;
}
```

**17. Concatenate Strings:**

```cpp
#include <cstdlib>
#include <cstring>

int main() {
  // Concatenate two strings using strcat
  strcat(destination, "Hello World");
  return 0;
}
```

**18. Convert String to Uppercase:**

```cpp
#include <cstdlib>
#include <cstring>

int main() {
  // Convert a string to uppercase using toupper
  char *upper = toupper("hello world");
  return 0;
}
```

**19. Convert String to Lowercase:**

```cpp
#include <cstdlib>
#include <cstring>

int main() {
  // Convert a string to lowercase using tolower
  char *lower = tolower("HELLO WORLD");
  return 0;
}
```

**20. Generate Random Password:**

```cpp
#include <cstdlib>
#include <ctime>

int main() {
  // Generate a random password of length 10
  char password[11];
  for (int i = 0; i < 10; i++) {
    password[i] = 'a' + rand() % 26;
  }
  return 0;
}
```

**21. Shuffle Array:**

```cpp
#include <cstdlib>
#include <random>

int main() {
  // Shuffle an array of integers
  std::random_device rd;
  std::mt19937 gen(rd());
  std::shuffle(std::begin(array), std::end(array), gen);
  return 0;
}
```

**22. Get Current Time:**

```cpp
#include <cstdlib>
#include <ctime>

int main() {
  // Get the current time as a time_t value
  time_t t = time(nullptr);
  return 0;
}
```

**23. Convert Time to String:**

```cpp
#include <cstdlib>
#include <ctime>

int main() {
  // Convert a time_t value to a string
  char *timeStr = ctime(&t);
  return 0;
}
```

**24. Sleep for a Specified Time:**

```cpp
#include <cstdlib>

int main() {
  // Sleep for 5 seconds
  sleep(5);
  return 0;
}
```

**25. Clear Screen:**

```cpp
#include <cstdlib>

int main() {
  // Clear the screen using system("cls") for Windows or system("clear") for Linux/macOS
  system("cls");
  return 0;
}
```

**26. Set Console Color:**

```cpp
#include <cstdlib>
#include <conio.h>

int main() {
  // Set the console text color to red
  textcolor(12);
  return 0;
}
```

**27. Get Console Input:**

```cpp
#include <cstdlib>
#include <conio.h>

int main() {
  // Get a single character from the console without echoing it
  char ch = _getch();
  return 0;
}
```

**28. Parse Command Line Arguments:**

```cpp
#include <cstdlib>

int main(int argc, char *argv[]) {
  // Print the first command line argument
  printf("First argument: %s\n", argv[1]);
  return 0;
}
```

**29. Execute System Command:**

```cpp
#include <cstdlib>

int main() {
  // Execute the "ls" command in the system
  system("ls");
  return 0;
}
```

**30. Get File Size:**

```cpp
#include <cstdlib>
#include <fstream>

int main() {
  // Get the size of a file
  std::ifstream file("myfile.txt");
  std::streampos begin = file.tellg();
  file.seekg(0, std::ios::end);
  std::streampos end = file.tellg();
  file.close();
  size_t size = end - begin;
  return 0;
}
```

**31. Read File Contents:**

```cpp
#include <cstdlib>
#include <iostream>
#include <fstream>

int main() {
  // Read the contents of a file
  std::ifstream file("myfile.txt");
  std::string line;
  while (std::getline(file, line)) {
    std::cout << line << std::endl;
  }
  file.close();
  return 0;
}
```

**32. Write to File:**

```cpp
#include <cstdlib>
#include <fstream>

int main() {
  // Write to a file
  std::ofstream file("myfile.txt");
  file << "Hello World!" << std::endl;
  file.close();
  return 0;
}
```

**33. Create Directory:**

```cpp
#include <cstdlib>
#include <filesystem>

int main() {
  // Create a directory named "mydir"
  std::filesystem::create_directory("mydir");
  return 0;
}
```

**34. Rename File:**

```cpp
#include <cstdlib>
#include <filesystem>

int main() {
  // Rename a file from "myfile.txt" to "newmyfile.txt"
  std::filesystem::rename("myfile.txt", "newmyfile.txt");
  return 0;
}

```
