that_one_nigga_you_k

tudo num só

Dec 21st, 2023
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.63 KB | None | 0 0
  1. /*questão 1*/
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. int main(){
  7.     char c = 'a';
  8.     char* pc = &c;
  9.  
  10.     printf("o endereço de ''c'' em hex: %p\no valor guardado em c é: %c\n\n", pc, c);
  11.  
  12.     printf("O valor que pc armazena e o valor guardado no endereço apontado por ele é: %p e ''%c''\n\n", pc, *pc);
  13.  
  14.     printf("o endereço de pc é: %p\n\n", &pc);
  15.  
  16.     printf("O endereço do valor guardado no endereço apontado por pc: %p \ne o valor guardado no endereço de pc: %p\n\n\n", &*pc, *&pc);
  17.  
  18.     /*eles são iguais por o *pc é a desreferencia do ponteiro pc, assim a gente vê para o q ele de fato ta apontando
  19.     o &pc nos dá o endereço da variável pc
  20.     pc -> c (apontando literalmente kkkk)
  21.     &*pc -> &c
  22.     &*pc = o endereço da variável q pc está apontando
  23.    
  24.    
  25.     no caso do *&pc temos algo mais ou menos assim
  26.    
  27.     &pc = endereço q o valor de pc está guardado
  28.     *&pc = o valor apontado pelo endereco de pc = c
  29.  
  30.     pois pois &pc obtém o endereço de pc e * obtém o valor armazenado nesse endereço, que é c
  31.  
  32.     isso é uma capoeira mental
  33.     */
  34.     return 0;
  35. }
  36.  
  37. /*questão 2*/
  38.  
  39. #include <stdio.h>
  40.  
  41. int main(){
  42.     int vet[5] = {1,2,3,4,5};
  43.     int* p = vet;
  44.  
  45.     printf("endereço (inicial de vet) e endereço guardado em p: %p, %p\n\n\n", &vet, p);
  46.  
  47.     printf("os valores de vet são: ");
  48.  
  49.     for(int i = 0; i < 5 ; i ++){
  50.         printf(" %d", *(p+i));
  51.     }
  52.     printf("\ne \n");
  53.     for(int i = 0; i < 5 ; i ++){
  54.         printf(" %d", p[i]);
  55.     }
  56.     printf("\n\n");
  57.     for(int i = 0; i < 5 ; i ++){
  58.         printf(" %d", vet[i]);
  59.     }
  60.     printf("\n\n");
  61.     for(int i = 0; i < 5 ; i ++){
  62.         printf(" %d", *(vet+i));
  63.     }
  64.  
  65.     printf("\n\n");
  66.     return 0 ;
  67. }
  68.  
  69. /*questão 3*/
  70.  
  71. #include <stdio.h>
  72. #include <string.h>
  73. int main( ){
  74. char str[50]; // String
  75. char str_inv[50]; // String invertida
  76. char *ptr_str = str;
  77. char *ptr_inv = str_inv;
  78. int i = -1;
  79. scanf(" %49s", str); // sem espaço
  80. // scanf(" %[^\n]", str);//com espacço
  81.  
  82. //printf("\n%d\n\n", strlen(str));
  83.  
  84.  
  85.  
  86. for (i; i < (int)strlen(str)-1; i++) {
  87.     *(ptr_inv + strlen(str) - i - 2) = *(ptr_str + i+1);   //fazer isso sem mudar nenhum valor foi foda, mas é só se ligar aonde começa e termina tudo
  88. }
  89.  
  90.     // Null-terminate the inverted string
  91. *(ptr_inv + strlen(str)) = '\0';
  92.  
  93. printf("O inverso da string: %s\n\n", str_inv);
  94.  
  95. return 0;
  96. }
  97.  
  98. /*questão 3 com função*/
  99.  
  100. #include <stdio.h>
  101. #include <string.h>
  102.  
  103. void inverter(char* ptr_inv, char* ptr_str, char* str, char* str_inv){
  104.     int i = - 1;
  105.    
  106.  
  107.     for (i; i < (int)strlen(str)-1; i++) {
  108.     *(ptr_inv + strlen(str) - i - 2) = *(ptr_str + i+1);   //fazer isso sem mudar nenhum valor foi foda, mas é só se ligar aonde começa e termina tudo
  109.     }
  110.  
  111.    
  112.     *(ptr_inv + strlen(str)) = '\0';
  113.  
  114. }
  115.  
  116.  
  117. int main( ){
  118. char str[50]; // String
  119. char str_inv[50]; // String invertida
  120. char *ptr_str = str;
  121. char *ptr_inv = str_inv;
  122. int i = -1;
  123. scanf(" %49s", str); // sem espaço
  124. // scanf(" %[^\n]", str);//com espacço
  125.  
  126.  
  127. inverter(ptr_inv, ptr_str, str, str_inv);
  128.    
  129. *(ptr_inv + strlen(str)) = '\0';
  130.  
  131. printf("O inverso da string: %s\n\n", str_inv);
  132.  
  133. return 0;
  134. }
  135.  
  136. /*questão 3 com recursividade*/
  137.  
  138. #include <stdio.h>
  139. #include <string.h>
  140.  
  141. void inverter(char* ptr_inv, char* ptr_str, int i, int len) {
  142.     if (i >= len)
  143.         return;
  144.  
  145.     *(ptr_inv + len - i - 1) = *(ptr_str + i + 1);
  146.  
  147.     inverter(ptr_inv, ptr_str, i + 1, len);
  148. }
  149.  
  150. int main() {
  151.     char str[50]; // String
  152.     char str_inv[50]; // String invertida
  153.     char *ptr_str = str;
  154.     char *ptr_inv = str_inv;
  155.     int i = -1;
  156.  
  157.     scanf(" %49s", str); // sem espaço
  158.  
  159.     inverter(ptr_inv, ptr_str, i, strlen(str) - 1);
  160.     *(ptr_inv + strlen(str)) = '\0';
  161.  
  162.     printf("O inverso da string: %s\n\n", str_inv);
  163.  
  164.     return 0;
  165. }
  166.  
  167. /*questão 4*/
  168.  
  169. #include <stdio.h>
  170. #include <string.h>
  171. #include <stdlib.h>
  172.  
  173. void ler(char*** str, int* indice) {
  174.     //ponteiro triplo q aponta para a matriz
  175.  
  176.     int flag = 1;
  177.  
  178.     while (flag) {
  179.  
  180.         /*daqui até o fim da função a gente manipluna da mesma forma, a diferença é q são os ponteiros apontando para os indices da matriz*/
  181.         (*str)[*indice] = (char*)malloc(1000 * sizeof(char));
  182.  
  183.         if ((*str)[*indice] == NULL) {
  184.             printf("deu errado essa porra ae");
  185.             flag = 0;
  186.         }
  187.  
  188.         printf("digite a string: \n");
  189.  
  190.         if (fgets((*str)[*indice], 1000, stdin) != NULL) {
  191.             if ((*str)[*indice][0] == '\n') {
  192.                 flag = 0;
  193.             }
  194.            
  195.             else {
  196.                 (*str)[*indice][strcspn((*str)[*indice], "\n")] = '\0';
  197.                 (*indice)++;
  198.                 *str = realloc(*str, (*indice + 1) * sizeof(char*));
  199.  
  200.                 if (*str == NULL) {
  201.                     printf("\n deu ruim isso aí ein\n");
  202.                     flag = 0;
  203.                 }
  204.             }
  205.         }
  206.        
  207.         else {
  208.             flag = 0;
  209.         }
  210.     }
  211. }
  212.  
  213. void mostrar(char*** str, int* indice){
  214.     printf("\nas strings\n");
  215.     for(int i = 0; i < *indice ; i++){
  216.         printf("%s\n", (*str)[i]);
  217.     }
  218. }
  219.  
  220. void apagar(char*** str, int* indice){
  221.     for(int i = 0; i < *indice ; i++){
  222.         printf("apagando a strins '%s'\n", (*str)[i]);
  223.         free((*str)[i]);
  224.     }
  225.     printf("apagando o resquicio da matriz\n");
  226.     free((*str));
  227. }
  228.  
  229.  
  230. int main(){
  231.     char** str = NULL;
  232.  
  233.     str = (char**)malloc(1 * sizeof(char*)); //malocando a quantidade de strings
  234.  
  235.     if(str == NULL){
  236.         printf("\ndeu mto ruim saporra\n");
  237.     }
  238.  
  239.     int indice = 0;
  240.  
  241.     ler(&str, &indice);
  242.    
  243.     mostrar(&str, &indice);
  244.  
  245.     apagar(&str, &indice);
  246.  
  247.     printf("\n\naté mais...\n\n");
  248.  
  249.     return 0;
  250. }
  251.  
  252. /*questão 4 com função para basicamente tudo*/
  253.  
  254. #include <stdio.h>
  255. #include <string.h>
  256. #include <stdlib.h>
  257.  
  258. void ler(char*** str, int* indice) {
  259.     //ponteiro triplo q aponta para a matriz
  260.  
  261.     int flag = 1;
  262.  
  263.     while (flag) {
  264.  
  265.         /*daqui até o fim da função a gente manipluna da mesma forma, a diferença é q são os ponteiros apontando para os indices da matriz*/
  266.         (*str)[*indice] = (char*)malloc(1000 * sizeof(char));
  267.  
  268.         if ((*str)[*indice] == NULL) {
  269.             printf("deu errado essa porra ae");
  270.             flag = 0;
  271.         }
  272.  
  273.         printf("digite a string: \n");
  274.  
  275.         if (fgets((*str)[*indice], 1000, stdin) != NULL) {
  276.             if ((*str)[*indice][0] == '\n') {
  277.                 flag = 0;
  278.             }
  279.            
  280.             else {
  281.                 (*str)[*indice][strcspn((*str)[*indice], "\n")] = '\0';
  282.                 (*indice)++;
  283.                 *str = realloc(*str, (*indice + 1) * sizeof(char*));
  284.  
  285.                 if (*str == NULL) {
  286.                     printf("\n deu ruim isso aí ein\n");
  287.                     flag = 0;
  288.                 }
  289.             }
  290.         }
  291.        
  292.         else {
  293.             flag = 0;
  294.         }
  295.     }
  296. }
  297.  
  298. void mostrar(char*** str, int* indice){
  299.     printf("\nas strings\n");
  300.     for(int i = 0; i < *indice ; i++){
  301.         printf("%s\n", (*str)[i]);
  302.     }
  303. }
  304.  
  305. void apagar(char*** str, int* indice){
  306.     for(int i = 0; i < *indice ; i++){
  307.         printf("apagando a strins '%s'\n", (*str)[i]);
  308.         free((*str)[i]);
  309.     }
  310.     printf("apagando o resquicio da matriz\n");
  311.     free((*str));
  312. }
  313.  
  314.  
  315. int main(){
  316.     char** str = NULL;
  317.  
  318.     str = (char**)malloc(1 * sizeof(char*)); //malocando a quantidade de strings
  319.  
  320.     if(str == NULL){
  321.         printf("\ndeu mto ruim saporra\n");
  322.     }
  323.  
  324.     int indice = 0;
  325.  
  326.     ler(&str, &indice);
  327.    
  328.     mostrar(&str, &indice);
  329.  
  330.     apagar(&str, &indice);
  331.  
  332.     printf("\n\naté mais...\n\n");
  333.  
  334.     return 0;
  335. }
  336.  
  337. /*questão 5*/
  338.  
  339. #include <stdio.h>
  340. #include <string.h>
  341. #include <stdlib.h>
  342.  
  343. void ler(char*** str, int* indice) {
  344.     //ponteiro triplo q aponta para a matriz
  345.  
  346.     int flag = 1;
  347.  
  348.     while (flag) {
  349.  
  350.         /*daqui até o fim da função a gente manipluna da mesma forma, a diferença é q são os ponteiros apontando para os indices da matriz*/
  351.         (*str)[*indice] = (char*)malloc(1000 * sizeof(char));
  352.  
  353.         if ((*str)[*indice] == NULL) {
  354.             printf("deu errado essa porra ae");
  355.             flag = 0;
  356.         }
  357.  
  358.         printf("digite a string: \n");
  359.  
  360.         if (fgets((*str)[*indice], 1000, stdin) != NULL) {
  361.             if ((*str)[*indice][0] == '\n') {
  362.                 flag = 0;
  363.             }
  364.            
  365.             else {
  366.                 (*str)[*indice][strcspn((*str)[*indice], "\n")] = '\0';
  367.                 (*indice)++;
  368.                 *str = realloc(*str, (*indice + 1) * sizeof(char*));
  369.  
  370.                 if (*str == NULL) {
  371.                     printf("\n deu ruim isso aí ein\n");
  372.                     flag = 0;
  373.                 }
  374.             }
  375.         }
  376.        
  377.         else {
  378.             flag = 0;
  379.         }
  380.     }
  381. }
  382.  
  383. void mostrar(char*** str, int* indice){
  384.     printf("\nas strings\n");
  385.     for(int i = 0; i < *indice ; i++){
  386.         printf("%s\n", (*str)[i]);
  387.     }
  388. }
  389.  
  390. void apagar(char*** str, int* indice){
  391.     for(int i = 0; i < *indice ; i++){
  392.         printf("apagando a strins '%s'\n", (*str)[i]);
  393.         free((*str)[i]);
  394.     }
  395.     printf("apagando o resquicio da matriz\n");
  396.     free((*str));
  397. }
  398.  
  399.  
  400. int main(){
  401.     char** str = NULL;
  402.  
  403.     str = (char**)malloc(1 * sizeof(char*)); //malocando a quantidade de strings
  404.  
  405.     if(str == NULL){
  406.         printf("\ndeu mto ruim saporra\n");
  407.     }
  408.  
  409.     int indice = 0;
  410.  
  411.     ler(&str, &indice);
  412.    
  413.     mostrar(&str, &indice);
  414.  
  415.     apagar(&str, &indice);
  416.  
  417.     printf("\n\naté mais...\n\n");
  418.  
  419.     return 0;
  420. }
Add Comment
Please, Sign In to add comment