Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int main(){
- char** str = NULL;
- str = (char**)malloc(1 * sizeof(char*)); //malocando a quantidade de strings
- if(str == NULL){
- printf("\ndeu mto ruim saporra\n");
- }
- int indice = 0;
- int flag = 1;
- while (flag) {
- str[indice] = (char*)malloc(1000 * sizeof(char));
- if (str[indice] == NULL) {
- printf("deu errado essa porra ae");
- flag = 0;
- }
- printf("digite a string: \n");
- //só consegui com fgets até agr
- if (fgets(str[indice], 1000, stdin) != NULL) {
- // vendo se o primeiro valor da string é a quebra de linha
- if (str[indice][0] == '\n') {
- flag = 0;
- }
- else{
- // tirando a quebra de linha
- str[indice][strcspn(str[indice], "\n")] = '\0';
- // aumentando o indice
- indice++;
- // realoc bom e veio de guerra
- str = realloc(str, (1 + indice) * sizeof(char *));
- if (str == NULL) {
- printf("\n deu ruim isso aí ein\n");
- flag = 0;
- }
- }
- } else {
- flag = 0;
- // cara apertou enter
- }
- }
- printf("\naí as strings\n\n");
- for(int i = 0 ; i < indice ; i++){
- printf("\n%s", str[i]);
- free(str[i]);
- }
- free(str);
- printf("\n\naté mais...\n\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment