Unconfigured Ad Widget

Collapse

Anúncio

Collapse
No announcement yet.

Ajuda em simulação da função strcat()

Collapse
X
 
  • Filter
  • Tempo
  • Show
Clear All
new posts

  • Font Size
    #1

    C / C++ Ajuda em simulação da função strcat()

    Antes d tudo comprimento a todos !

    Estava tentando simular a função strcat() e deu nisso aki em baixo, alguem pode me ajudar, me falar na onde errei, e também se possivel cirar a função strlen().
    #include <stdio.h>
    #include <string.h>
    void StrCat(char *dest,char *orig){
    while(*dest!='\0') dest++;
    *(dest+1)=" ";
    dest++;
    while(*orig!='\0'){
    *dest=*orig;
    dest++;
    orig++;
    }
    }
    int main(){
    char str1[100],str2[100];
    printf("\nEntre com uma string: ");
    gets(str1);
    strcpy(str2,"Voce digitou o valor: ");
    StrCat(str2,str1);
    printf("\n%s\n",str2);
    printf(" FIM... ");
    return(0);
    }
    Obg !
    Similar Threads

  • Font Size
    #2
    Só comentei 2 linhas do teu código e funcionou legal. Tomei a liberdade e adicionei minha propria versão do strcat() e uma versão do strlen(). Ah também tirei o gets() e coloquei o scanf(), que é mais "seguro".

    Código:
    #include <stdio.h>
    #include <string.h>
    
    void StrCat(char *dest, char *orig) {
        while (*dest != '\0') 
            dest++;
        //*(dest + 1) = " ";
        //dest++;
        while (*orig != '\0') {
            *dest = *orig;
            dest++;
            orig++;
        }
        *dest = '\0';
    }
    
    void StrCat2(char *d, char *o) {
        char * aux = d;
        char * bux = o;
        
        while(*aux)
            aux++;
        while(*bux){
            *aux = *bux;
            aux++;
            bux++;
        }
        *aux = '\0';        // O \0 não é copiado, por isso temos que o adicionar a mão
    }
    
    int StrLen(const char *s) {
        char * str = s;
        
        while(*str)
            str++;
        return (int) (str - s);
    }
    
    int main() {
        char str1[100], str2[100];
        
        printf("\nEntre com uma string: ");
        scanf("%s", str1);
        strcpy(str2, "Voce digitou o valor: ");
        StrCat(str2, str1);
        printf("%s\n", str2);
        printf("Comprimento: %d\n", StrLen(str1));
        printf("FIM...\n");
        
        return (0);
    }
    “Finalmente encontrei um inimigo digno de mim e uma jornada em que preciso desenvolver toda a minha coragem, pois temos de combater homens bravos e monstruosas feras.”, Alexandre, o Grande.

    Comment


    • Font Size
      #3
      vlw aew cara, tava com essa duvida, simples né, vo te add flw.

      Comment


      • Font Size
        #4
        Agora sim tá Perfect, só com um aviso de não haver nova linha no final do arquivo (ou quase)
        #include <stdio.h>

        void StrCat(char *dest,char *orig){
        while(*dest) *dest++;
        while(*orig){
        *dest=*orig;
        *dest++;
        *orig++;
        }
        }

        int StrLen(char *str){
        char *fix=str;
        while(*str) str++;
        return (int) (str - fix);
        }

        int main(){
        char str1[100],str2[100]="Voce digitou: ";
        printf("\nEntre com uma string: ");
        gets(str1);
        StrCat(str2,str1);
        printf("\n%s\nCaracteres: %d\n FIM...\n",str2,StrLen(str1));
        return(0);
        }

        Comment

        X
        Working...
        X