Concatenate String in C without use of strcat () function

0

I need a help, I needed to solve this programming language exercise, I would concatenate a string, in case STRING_B in STIRNG_A, without using a function ready, by pointers method.

example:

ENTRADAS >> "CASA " - "BAHIA"

SAIDA >> CASA BAHIA

follow the code ...

#include <stdio.h>
#include <string.h>
/*escreva uma funcao em c que recebe a referencia de duas strings e concatena
a segunda na primeira sem usar funcao strcat()*/

void ptr(char *string_a, char *string_b)
{
    int i;
    do{
        string_a++;
    }while(*string_a!='
ENTRADAS >> "CASA " - "BAHIA"

SAIDA >> CASA BAHIA
'); do{ string_a=*string_b; string_b++; }while(*string_b!='
#include <stdio.h>
#include <string.h>
/*escreva uma funcao em c que recebe a referencia de duas strings e concatena
a segunda na primeira sem usar funcao strcat()*/

void ptr(char *string_a, char *string_b)
{
    int i;
    do{
        string_a++;
    }while(*string_a!='%pre%');
    do{
        string_a=*string_b;
        string_b++; 
    }while(*string_b!='%pre%');
}
int main (void)
{
    char string_a[250];
    char string_b[250];
    puts(" -- DIGITE UMA PALAVRA -- ");
    gets(string_a);
    puts(" -- DIGITE UMA PALAVRA -- ");
    gets(string_b);
    ptr(string_a,string_b);
    puts(string_a);
    return 0;
}
'); } int main (void) { char string_a[250]; char string_b[250]; puts(" -- DIGITE UMA PALAVRA -- "); gets(string_a); puts(" -- DIGITE UMA PALAVRA -- "); gets(string_b); ptr(string_a,string_b); puts(string_a); return 0; }
    
asked by anonymous 19.08.2017 / 18:46

1 answer

1

You do not have to use do_while and then increment, if so, your code is a bit confusing for me, I did it in a slightly simpler way:

char* ptr(char *string_a, char *string_b)
{
    int i;
   for(i=0; string_a[i]!='
char* ptr(char *string_a, char *string_b)
{
    int i = 0, j = 0;
    while(string_a[i]!='
char* ptr(char *string_a, char *string_b)
{
    int i;
   for(i=0; string_a[i]!='
char* ptr(char *string_a, char *string_b)
{
    int i = 0, j = 0;
    while(string_a[i]!='%pre%') { ++i; }
    while(string_b[j]!='%pre%') 
    {
        string_a[i]=string_b[j];
        j++; i++;
    }
    string_a[i]='%pre%';
    return string_a;
}
'; ++i); //Percorre toda a string_a, para saber o tamanho da mesma for(int j=0; string_b[j]!='%pre%'; ++j, ++i) //Percorre toda a string_b, para concatena-la { string_a[i]=string_b[j]; } string_a[i]='%pre%'; return string_a; } int main (void) { char string_a[250]; char string_b[250]; puts(" -- DIGITE UMA PALAVRA -- "); gets(string_a); puts(" -- DIGITE UMA PALAVRA -- "); gets(string_b); puts(ptr(string_a,string_b)); return 0; }
') { ++i; } while(string_b[j]!='%pre%') { string_a[i]=string_b[j]; j++; i++; } string_a[i]='%pre%'; return string_a; }
'; ++i); //Percorre toda a string_a, para saber o tamanho da mesma for(int j=0; string_b[j]!='%pre%'; ++j, ++i) //Percorre toda a string_b, para concatena-la { string_a[i]=string_b[j]; } string_a[i]='%pre%'; return string_a; } int main (void) { char string_a[250]; char string_b[250]; puts(" -- DIGITE UMA PALAVRA -- "); gets(string_a); puts(" -- DIGITE UMA PALAVRA -- "); gets(string_b); puts(ptr(string_a,string_b)); return 0; }

See working at Ideone .

With while:

%pre%

See working at Ideone .

    
19.08.2017 / 19:05