Strcat Function In C++
Répondre à la discussion
Affichage des résultats 1 à 3 sur 3

Strcat Function In C++



  1. #1
    invitec4a879f2

    Strcat Function In C++


    ------

    I'm new to C and C++ programming, can anyone give me a hint on what I'm doing wrong here. I'm trying to write to concat function that takes to pointers to chars and concatenates the second to the first. The code does do that, but the problem is that it adds a bunch of junk at the end.

    For instance, when passing the arguments - "green" and "blue", the output will be "greenblue" plus a bunch of random characters. I also wrote the strlen function that strcat uses, which I will provide below it for reference. I'm using the online compiler at

    ###### The link given is false, it has been suppressed
    and specification is this:

    Code:
    int main(int argc, char** argv)
    {
    const int MAX = 100;
    
    char s1[MAX];
    char s2[MAX];
    
    cout << "Enter your first string up to 99 characters. ";
    cin.getline(s1, sizeof(s1));
    int size_s1 = strlen(s1);
    cout << "Length of first string is " << size_s1 << "\n";
    
    cout << "Enter your second string up to 99 characters. ";
    cin.getline(s2, sizeof(s2));
    int size_s2 = strlen(s2);
    cout << "Length of second string is " << size_s2 << "\n";
    cout << " Now the first string will be concatenated with the second
    string ";
    char* a = strcat(s1,s2);
    
    for(int i = 0; i<MAX; i++)
    cout <<a[i];
    
    // system("pause");
    return 0;
    }
    
    //strcat function to contatenate two strings
    char* strcat(char *__s1, const char *__s2)
    {
    int indexOfs1 = strlen(__s1);
    int s2L = strlen(__s2);
    cout <<s2L << "\n";
    int indexOfs2 = 0;
    do{
    __s1[indexOfs1] = __s2[indexOfs2];
    indexOfs1++;
    indexOfs2++;
    }while(indexOfs2 < s2L);
    
    
    return __s1;
    }
    
    //Returns length of char array
    size_t strlen(const char *__s)
    {
    int count = 0;
    int i;
    for (i = 0; __s[i] != '\0'; i++)
    count++;
    return (count) / sizeof(__s[0]);
    
    }

    -----
    Dernière modification par gienas ; 05/01/2022 à 13h43. Motif: Suppression of wrong link

  2. #2
    gienas
    Modérateur

    Re : Strcat Function In C++

    Hello Kamalsharma and everybody

    Welcome on Futura forums.

    Citation Envoyé par Kamalsharma Voir le message
    I'm new to C and C++ programming . .
    1- the langage to be used on the forum is only French. May be no future response;

    2- it's not a technology queestion, it will be placed on the correct section, about programming;

    3- you used a link non valid which has been suppressed.

  3. #3
    umfred

    Re : Strcat Function In C++

    pourquoi réécrire une fonction qui existe déjà ? https://www.cplusplus.com/reference/cstring/strcat/
    potentiellement, il y a un problème de taille des chaines; sinon une chaine se termine toujours par un caractère nul \0; ce que tu as "oublié" de faire après ta boucle.

Discussions similaires

  1. S-Function
    Par inviteafd3183f dans le forum Logiciel - Software - Open Source
    Réponses: 0
    Dernier message: 02/06/2015, 12h25
  2. function
    Par lena9 dans le forum Programmation et langages, Algorithmique
    Réponses: 3
    Dernier message: 01/05/2015, 11h58
  3. s-function de matlab
    Par invite7fb0a66f dans le forum Logiciel - Software - Open Source
    Réponses: 1
    Dernier message: 22/04/2009, 06h31
  4. qu'est-ce que le ''I-function''
    Par invitebffa0846 dans le forum Biologie
    Réponses: 3
    Dernier message: 16/11/2005, 08h56
  5. Aide sur les S-Function
    Par invitea77fee56 dans le forum Logiciel - Software - Open Source
    Réponses: 1
    Dernier message: 23/04/2005, 11h49