hello everyone
i was trying to make my own own string concatenation function but wasnt able to do so. i need ur help.

Dani AI

Generated

Nice start, . A few core issues are tripping you up:

  • Use the null character '\0', not '/0'. That typo appears in multiple places and will keep loops from stopping.
  • Your line with cout << ... << puts(str3); explains the output you saw: puts prints the string and returns an int, so you end up streaming that return value (often 0 or another number). Either do std::cout << str3 << '\n'; or call puts(str3); by itself.
  • The for-loop scoping warning means you declared i/j inside the loop, then tried to use them after the loop ended. In standard C++, that is out of scope. If you need them later, declare them before the loop. pointed you in the right direction.

If the goal is a custom strcat, make it safe and return the destination pointer just like the real one. The standard strcat assumes the destination has enough space; this version takes the buffer size to prevent overflow (and always null-terminates).

// needs: #include <cstring>
char* my_strcat(char* dest, const char* src, std::size_t dest_size) {
    std::size_t dlen = std::strlen(dest);
    if (dlen >= dest_size) return dest;            // no room even for '\0'

    std::size_t i = 0;
    while (src[i] != '\0' && dlen + i + 1 < dest_size) {
        dest[dlen + i] = src[i];
        ++i;
    }
    dest[dlen + i] = '\0';
    return dest;
}

Example usage (no gets, no conio):

// needs: #include <iostream>, <cstring>
char a[50], b[50], out[100] = {0};
std::cin.getline(a, sizeof a);
std::cin.getline(b, sizeof b);
std::strcpy(out, a);
my_strcat(out, b, sizeof out);
std::cout << "concatenated: " << out << '\n';

For reference on behavior and preconditions of strcat, see cppreference: strcat. Also note that gets was removed from the C standard because it cannot be used safely; prefer std::cin.getline (C++) or fgets (C) instead: cppreference: gets.

Recommended Answers

All 11 Replies

hello everyone
i was trying to make my own own string concatenation function but wasnt able to do so. i need ur help.

What don't you understand that prevents you from doing this? What do you think would be a subroutine that does work?

What don't you understand that prevents you from doing this? What do you think would be a subroutine that does work?

by this time i converted it to a prog

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
char m,str1[50],str2[50],str3[100];

int main()
{
    
    int l=0;
    gets(str1);
    gets(str2);
    for(int i=0;str1[i]!='/0';i++)
    {str3[i]=str1[i];
    l=l+1;}
    for(int j=0;str2[j]=!'/0';j++)
    str3[l+j]=str2[j];
    
    str3[i+j]='/0';
    cout<<" the concatinated str is "<<puts(str3);
    getch();
    return 0;
}

plz debug it

It should be

str2[j]!='\0'

not

str2[j]=!'/0'

change '/0' in first for loop also...use fgets instead of gets

It should be

str2[j]!='\0'

not

str2[j]=!'/0'

change '/0' in first for loop also...use fgets instead of gets

well sunny i did wat u said
but m still getting errors n warning . here they are --
Compiler: Default compiler
Executing
In file included from C:/Dev-Cpp/include/c++/3.4.2/backward/iostream.h:31,
from C:\Dev-Cpp\concatination.cpp:1:
C:/Dev-Cpp/include/c++/3.4.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.
C:\Dev-Cpp\concatination.cpp: In function `int main()':
C:\Dev-Cpp\concatination.cpp:18: error: name lookup of `i' changed for new ISO `for' scoping
C:\Dev-Cpp\concatination.cpp:12: error: using obsolete binding at `i'

C:\Dev-Cpp\concatination.cpp:18: error: name lookup of `j' changed for new ISO `for' scoping
C:\Dev-Cpp\concatination.cpp:15: error: using obsolete binding at `j'

Execution terminated

Here's the corrected code....stop mixing c headers with c++ headers....don't use ancient headers like <iostream.h>...instead use <iostream>.....declare variable outside for loop...not allowed according to latest ISO standard

#include<iostream>
#include<conio.h>
using namespace std;
char m,str1[50],str2[50],str3[100];

int main()
{

int i,j,temp,l=0;
gets(str1);
gets(str2);

for(i=0;str1[i]!='\0';i++)
{
        str3[i]=str1[i];
        l=l+1;
}
for(j=0;str2[j]!='\0';j++)
        str3[l+j]=str2[j];

str3[i+j]='\0';
cout<<" the concatinated str is "<<puts(str3);
getch();
return 0;
}

Next Time Use this to stop the output window in DEV instead of getch() which requires <conio.h>

#include <iostream>
#include <limits>

using namespace std;

int main()
{
  // Your code here

  // Clean up the stream and ask for input
  cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
  cin.get();
}

m getting thisas output n not wat i want , also can u now convert it to a function like strcat()
as
fg
asfg
the concatinated str is 0

plz debug this prog to make a matrix

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main()
{
    int ar[3][4];
    for(int i=0;i<3;i++)
    {
       for(int j=0;j<4;j++)
       {
          cin>>ar[i][j];
          
       }
    }
    
    for(int k=0;k<3;k++)
      {
            for(int p=0;p<4;p++)
            {
                    cout<<ar[i][j]<<"/t";
                    
            }
      }
return 0;
}

plz find the error n correct it

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main()
{
    char str[20],l=0,flag=1;
    cout<<"enter the string " ;
    gets(str);
    for(int i=0;str[i]!='/0';i++)
    l++;
    for(int j=0;j<=(l-1)/2;j++)
    {
            
            if(str[j]==str[l-1])
            flag=1;
            else break;                        
    }
     if(flag==1)
     cout<<" string is a palindrome ";
     else cout<< " string is not a palindrome ";
     getch();
     
}

//palindromes = nitin,malayalam ,madam etc

whatever code u have written in main...write it in another function and call that function by passing the indivisual strings and return the concatenated string from it...
Try searching a bit before asking....u'll find it in this forum itself

dont debug that matrix one . did it myself

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int main()
{
    int ar[3][4];
    for(int i=0;i<3;i++)
    {
       for(int j=0;j<4;j++)
       {
          cin>>ar[i][j];
          
       }
    }
    cout<<endl;
    for(int k=0;k<3;k++)
      {
            for(int p=0;p<4;p++)
            {
                    cout<<ar[k][p]<<"\t";
                    
            }
            cout<<endl;
      }
getch();
return 0;
}

As for the strcat, this is in the Code Snippets:
Strings: Concatenation

[edit]And never use gets.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.