954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

creating our own C++ strcat() function, code reqd

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

sahil_logic
Light Poster
31 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 
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?

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 
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

sahil_logic
Light Poster
31 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

It should be

str2[j]!='\0'


not

str2[j]=!'/0'

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

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

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 17.4.1.2 of the C++ standard. Examples include substituting the header for the header for C++ includes, or instead of the deprecated header . 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

sahil_logic
Light Poster
31 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

Here's the corrected code....stop mixing c headers with c++ headers....don't use ancient headers like ...instead use .....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 #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();
}

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

[/code][/QUOTE]
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

sahil_logic
Light Poster
31 posts since Nov 2005
Reputation Points: 10
Solved Threads: 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;
}
sahil_logic
Light Poster
31 posts since Nov 2005
Reputation Points: 10
Solved Threads: 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

sahil_logic
Light Poster
31 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

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

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

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;
}
sahil_logic
Light Poster
31 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

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

[edit]And never use <a href="http://www.eskimo.com/~scs/C-faq/q12.23.html">gets</a> .

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You