Jump to content

Armazenar uma string dentro de outra string


Giovanna Moreira

Recommended Posts

Faça um programa que leia cinco palavras pelo teclado, e armazene cada palavra em uma string. Depois, copie somente as posições pares para outras 5 string.  Por fim imprima as 10 strings.

Consegui gerar apenas os caracteres das posições pares mas não consigo colocá-los dentro de uma string, alguém pode me ajudar?

 

#include <stdio.h>

#include <string.h>

#define TAM 15

int main()

{

char str1[TAM], str2[TAM], str3[TAM], str4[TAM], str5[TAM], str6[TAM];

char str[TAM*5], i=0;


 

/* Le todas as strings */

printf("\n\n Digite 5 strings, teclando <ENTER> ao final de cada uma:\n");

gets(str1);

gets(str2);

gets(str3);

gets(str4);

gets(str5);


 

str[0] = '\0';

 

printf("\n");

printf("\n");

  for(i=0; str1 !='\0'; i++)

 

  {

    if(i%2==0)

    printf("%c", str1);    

   

  }

 

  printf("\n");

  printf("\n");

  for(i=0; str2 !='\0'; i++)

 

  {

    if(i%2==0)

    printf("%c", str2);        

  }

 

  printf("\n");

  printf("\n");

  for(i=0; str3 !='\0'; i++)

 

  {

    if(i%2==0)

    printf("%c", str3);        

  }

 

  printf("\n");

  printf("\n");

  for(i=0; str4 !='\0'; i++)

 

  {

    if(i%2==0)

    printf("%c", str4);        

  }

 

  printf("\n");

  printf("\n");

  for(i=0; str5 !='\0'; i++)

 

  {

    if(i%2==0)

    printf("%c", str5);        

  }

  

return(0);

}

Link to comment
Share on other sites

Para conseguir fazer o que você quer, ao invés de imprimir os caractéres das posições pares no seu for, você tem que copiá-los em uma nova string e somente depois disso você vai imprimir as 10 strings.

 

Ao invés de:

`````{

    if(i%2==0)

    printf("%c", str1);       

  }

você faz (por exemplo):

{

 str6[I/2] = str1;

}

printf("%c", str1); 

printf("%c", str6); 

Mas você para funcionar você precisa declarar as "novas" strings alocando corretamente o espaço da memória e além disso você tem que faz um loop para cada string, porque elas podem ter tamanhos diferentes.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...