-
Help em Strings
Boas
tenho de fazer um exercício em que pede uma string ao utilizador e depois constroi uma nova string retirando a primeira letra e depois a ultima sucessivamente (de fora para dentro)
exemplo Batatas -> Bsaatta
já fiz muitas tentativas mas não estou a conseguir fazer isto (sou iniciado em c)
o que tenho até agora é isto:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TAMANHO 60
int main() {
char nome[TAMANHO];
char nomeCodificado[TAMANHO];
int tamanho,i,d,j;
printf("Indique um nome: ");
gets(nome);
tamanho = strlen(nome);
d = tamanho;
d=d-1;
j=0;
for(i=0; i<tamanho; i++){
printf("entra ciclo\n i =%i ",i);
nomeCodificado[i] = nome[i];
printf("Pos: %i Copia : %c\n",i,nome[i]);
printf("Pos: %i Escreve: %c\n",i,nomeCodificado[i] );
j=i+1;
nomeCodificado[j] = nome[d];
printf("Pos: %i Copia : %c\n",d,nome[d]);
printf("Pos: %i Escreve: %c\n",j,nomeCodificado[j] );
d--;
}
printf("Resultado: %s\n",nomeCodificado);
system("PAUSE");
return 0;
}
podem dar uma ajuda?
-
You're going to have to post this in English if you want much help....
-
hi
I have to do an exercise that asks the user to give a name "string" and then build a new string by removing the first letter and the last letter and so on..
exemple
Batata -> Bsaatta
already made many attempts but I am not able to do this (I started at c)
tanks
-
So that's what you need to do then. Figure out the first letter, and put it somewhere. Then figure out the last letter, and put it somewhere. Move forward a space and backward a space from your previous positions to get the next ones.
-
sounds easy.
But, is not somewhere, the new string is build from left to right,
and in the second cycle the i uses the second position than is already used by j
Code:
for(i=0; i<tamanho; i++){
printf("entra ciclo\n i =%i ",i);
nomeCodificado[i] = nome[i];
printf("Pos: %i Copia : %c\n",i,nome[i]);
printf("Pos: %i Escreve: %c\n",i,nomeCodificado[i] );
j=i+1;
nomeCodificado[j] = nome[d];
printf("Pos: %i Copia : %c\n",d,nome[d]);
printf("Pos: %i Escreve: %c\n",j,nomeCodificado[j] );
d--;
}
-
You should probably keep track of where you are in your copied string then, instead of trying to use the same variable that's being used for something else.