Skip to content

Instantly share code, notes, and snippets.

@BruJu
Created May 19, 2020
Embed
What would you like to do?
// Some functions I coded to help a friend that was
// enrolled in an Engineering School
// 2019-05-19
#include <stdio.h>
#include <stdlib.h>
char ** reverse(int argc, char * argv[]) {
char ** new_argv = malloc(sizeof(char *) * argc);
for (int i = 0 ; i != argc ; i++) {
new_argv[i] = argv[argc - 1 - i];
}
return new_argv;
}
int size_of_string(char * str) {
int l = 0;
while (str[l]) {
l++;
}
return l;
}
void copy_string(char * dst, int * i_write, char * src) {
int i = 0;
while (src[i]) {
dst[*i_write] = src[i];
*i_write = *i_write + 1;
i++;
}
}
char * sum_of_param(int argc, char * argv[]) {
int size = 0;
for (int i = 0 ; i != argc ; i++) {
size += size_of_string(argv[i]);
}
char * concat = malloc(sizeof(char) * (size + 1));
int i_write = 0;
for (int i = 0 ; i != argc ; i++) {
copy_string(concat, &i_write, argv[i]);
}
concat[size] = 0;
return concat;
}
int main_sop(int argc, char * argv[]) {
printf("<%s>\n", sum_of_param(argc, argv));
return 0;
}
/*
@DESKTOP-IDJICF1:/mnt/b/azaf$ ./a abou arr 456 2ra 1
<./aabouarr4562ra1>
*/
int mainreverse(int argc, char * argv[]) {
char ** rev = reverse(argc, argv);
for (int i = 0 ; i != argc ; i++) {
printf("%s ", rev[i]);
}
printf("\n");
return 0;
}
//
int **my_reverse_arr2(int **arr, unsigned int size_x, unsigned int size_y) {
// Initialization
int ** res = malloc(sizeof(int) * size_y);
for (int i = 0 ; i != size_y ; i++) {
res[i] = malloc(sizeof(int) * size_x);
}
// Fill
for (int i = 0 ; i != size_y ; i++) {
for (int j = 0 ; j != size_x ; j++) {
res[i][j] = arr[j][i];
}
}
return res;
}
void printtab(int ** tab, int x, int y) {
for (int i = 0 ; i != x ; i++) {
for (int j = 0 ; j != y ; j++) {
printf("%d ", tab[i][j]);
}
printf("\n");
}
}
int main(int argc, char * argv[]) {
int n = 0;
int x = 3;
int y = 2;
int ** origine = malloc(sizeof(int) * x);
for (int i = 0 ; i != x ; i++) {
origine[i] = malloc(sizeof(int) * y);
for (int j = 0 ; j != y ; j++) {
origine[i][j] = n++;
}
}
int ** transpoee = my_reverse_arr2(origine, x, y);
printtab(origine, x, y);
printf("\n");
printtab(transpoee, y, x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment