Thread: Help em Strings

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    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?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You're going to have to post this in English if you want much help....

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    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

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    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--;
        }

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM