0% found this document useful (0 votes)
6 views4 pages

C++ String and Array Functions

The document contains code snippets for various string and array manipulation functions including reversing a string, changing case, substring extraction, finding all indexes of a substring, finding the maximum sum column in a 2D array, checking matrix symmetry, and calculating the difference between diagonals. Additional functions calculate factorials, file sums, and check if a number is complete.

Uploaded by

thienvp01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

C++ String and Array Functions

The document contains code snippets for various string and array manipulation functions including reversing a string, changing case, substring extraction, finding all indexes of a substring, finding the maximum sum column in a 2D array, checking matrix symmetry, and calculating the difference between diagonals. Additional functions calculate factorials, file sums, and check if a number is complete.

Uploaded by

thienvp01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

void reverse(char str[]){

int length = 0;
//tim do dai
while (str[length] != '\0'){
length++;
}

//hoan doi
for (int i = 0; i < length / 2; i++){
char temp = str[i];
str[i] = str[length - i - 1];
str[length - i - 1] = temp;
}
}

void recover(char signal[]){


for (int i = 0; signal[i] != '\0'; i++){
//chuyen thanh chu in hoa
if (signal[i] >= 'a' && signal[i] <= 'z'){
signal[i] = signal[i] - 32;
}
//chuyen thanh chu thuong
else if (signal[i] >= 'A' && signal[i] <= 'Z'){
signal[i] = signal[i] + 32;
}
}
}

void cutString(std::string s, int index){


if (index >= 0 && index < [Link]()){
string sub = [Link](index);
cout <<sub <<endl;
}
}

void findAllIndex(string s1, string s2) {


// tim vi tri dau tien cua s2 trong s1
size_t pos = [Link](s2);

//khong tim thay thi in ra -1


if (pos == string::npos){
cout << -1;
} else {
//in ra vi tri dau tien tim thay
cout << pos;
while ((pos = [Link](s2, pos + 1)) != string::npos) {
cout << " " << pos;
}
}
}

int findMaxColumn(int arr[][1000], int row, int col){


int maxSum = INT_MIN;
int maxColIndex = -1;
for (int j = 0; j < col; j++){
int sum = 0;
for (int i = 0; i < row; i++){
sum += arr[i][j];
}
if (sum > maxSum){
maxSum = sum;
maxColIndex = j;
}
}

return maxColIndex;
}

int diagonalProduct(int arr[][1000], int row, int col){


int product = 1;
for (int i = 0; i < row; i++){
product *= arr[i][i];
}
return product;
}

bool isSymmetric(int arr[][1000], int row, int col){


for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
if (arr[i][j] != arr[j][i]){
return false;
}
}
}
return true;
}

int diagonalDiff(int arr[][1000], int row, int col, int x, int y){
int tongcheochinh = 0, tongcheophu = 0;
int _x = x, _y = y;
//cheochinhphiatren
while((x >= 0 && x < row) && (y >= 0 && y < col )){
tongcheochinh += arr[x][y];
x--;
y--;
}
x = _x;
y = _y;
//cheochinhphiaduoi
while((x >= 0 && x < row) && (y >= 0 && y < col )){
tongcheochinh+=arr[x][y];
x++;
y++;
}
tongcheochinh -= arr[_x][_y];
x = _x;
y = _y;
//cheophuphiaduoi
while((x >= 0 && x < row) && (y >= 0 && y < col )){
tongcheophu +=arr[x][y];
x++;
y--;
}
x = _x;
y = _y;
//cheophuphiatren
while((x >= 0 && x < row) && (y >= 0 && y < col )){
tongcheophu+=arr[x][y];
x--;
y++;
}
tongcheophu -= arr[_x][_y];
return abs(tongcheochinh-tongcheophu);
}

#include <fstream>
#include <sstream>
void calSum(string fileName) {
ifstream file(fileName);
int sum = 0;
string line;
while (getline(file, line)) {
istringstream iss(line);
int num;
while (iss >> num) {
if (num >= 0) {
sum += num;
}
}
}
[Link]();
cout <<sum << endl;
}

#include <iostream>

using namespace std;


// implement calculate factorial function in here
int giaithua(int n){
int giaithua=1;
for(int i=1; i<=n; i++){
giaithua *= i;
}
return giaithua;
}

int main(int narg, char** argv)


{
int N;
cin >> N;
long result;
// call function calculateFactorial in here and assign value to the variable
result
result = giaithua(N);

cout << result << endl;


return 0;
}

void sum2(int * array, int numEls, int &result)


{
result = array[0];
for(int i =1; i<numEls; i++){
result += array[i];
}
}

bool completeNum(int N)
{
int sum=0;
for(int i=1; i <N; i++){
if(N%i==0){
sum=sum + i;
}
}
if(N==sum){
return true;
}
return false;
}

You might also like