The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Trying to write my own Strcat function
Discuss Trying to write my own Strcat function in the C Programming forum on Dev Shed. Trying to write my own Strcat function C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 2nd, 2012, 01:13 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 5
Time spent in forums: 2 h 11 m 50 sec
Reputation Power: 0
|
|
|
Trying to write my own Strcat function
Hello everyone. This is my first post here, and I was wondering if any of you could help me with my problem.
I've been trying to get pointer's down and decided to re-write some predefined c++ functions like strcpy, and strcat.
only problem is I've had trouble with strcat
here's what I have so far. I just cant figure out what I actually need to do to concatenate the string2 onto the null character of string1 in my "Mystrcat" function.
any help would be greatly appreciated
#include<iostream>
using std::cin;
using std::cout;
const int SIZE_1 = 101;
const int SIZE_2 = 202;
void GetStrings(char * src, char * dest);
int MyStrLen(char * src, int & length);
void MyStrCat (int length, char * src, char * dest);
int main()
{
char string1[SIZE_1];
char * src = string1;
char string2[SIZE_1];
char * dest = string2;
int length = 0;
GetStrings(string1, string2);
MyStrLen(string1, length);
MyStrCat(length, src, dest);
return 0;
}
void GetStrings(char * src, char * dest)
{
cout << "Enter first Cstring (100 characters max): ";
cin >> src;
cout << "Enter second Cstring (100 characters max): ";
cin>> dest;
}
int MyStrLen(char * src, int & length)
{
int i = 0;
while (src[i] != '\0')
{
i++;
length++;
}
return length;
}
void MyStrCat (int length, char * src, char * dest)
{
//need help here!!
}
|

October 2nd, 2012, 01:50 PM
|
 |
Contributing User
|
|
|
|
Code:
/* if this question pertained to standard old plain c, which it does not. */
/* still, you asked about pointers. */
/* looks dangerous. It is! */
#include<stdio.h>
char*mystrcat(char*dest,char*src) {
char*a;
for (a = dest; *a; ++a) /* find the end of dest */
;
for (; *src; ++a, ++src) /* copy from source onto destination */
*a = *src;
*a = 0; /* set the nul byte! */
return dest;
}
int main() {
char a[88];
a[0] = 0;
puts(mystrcat(a,"Rose"));
puts(mystrcat(a," is a rose"));
puts(mystrcat(a," is a rose."));
return 0;
}
__________________
[code] Code tags[/code] are essential for python code!
|

October 2nd, 2012, 01:58 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
To start with, use code tags to list your code. Without code tags, the browser will strip out all the leading whitespace, which will also remove all your formatting and reduce your code's readability. Just preface your code listing with [code] and end it with [/code], thus:
Code:
#include<iostream>
using std::cin;
using std::cout;
const int SIZE_1 = 101;
const int SIZE_2 = 202;
void GetStrings(char * src, char * dest);
int MyStrLen(char * src, int & length);
void MyStrCat (int length, char * src, char * dest);
int main()
{
char string1[SIZE_1];
char * src = string1;
char string2[SIZE_1];
char * dest = string2;
int length = 0;
GetStrings(string1, string2);
MyStrLen(string1, length);
MyStrCat(length, src, dest);
return 0;
}
void GetStrings(char * src, char * dest)
{
cout << "Enter first Cstring (100 characters max): ";
cin >> src;
cout << "Enter second Cstring (100 characters max): ";
cin>> dest;
}
int MyStrLen(char * src, int & length)
{
int i = 0;
while (src[i] != '\0')
{
i++;
length++;
}
return length;
}
void MyStrCat (int length, char * src, char * dest)
{
//need help here!!
}
You should also disable smilies, especially since you'll be posting C++ source code, which smilies especially like to frak up something terrible.
In order to concatenate, you need to know how to copy a string. I'm used to using pointers, but it looks like you're using array indexing which will work just as well. You have one index for the source string and one index for the destination. When you copy a string, both indices will be the same, but when you concatenation then they will be different (unless you're concatenating onto an empty string). I'm assuming here that dest is the string being concatenated unto and src is the string to be added onto the end of dest, such that when you're finished the result will be in dest. So you start with both arrays' indices being the same, which is zero. You find the null-terminator of dest, so now dest's index will be different. Now you copy src into dest incrementing both indices after you copy a character from srce to dest. And when you're finished, you add a null-terminator onto the resultant string in dest.
Just make sure that dest is large enough to hold the resultant concatenation-plus-null-terminator, which is always the responsibility of the programmer using strcat.
|

October 2nd, 2012, 02:32 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 5
Time spent in forums: 2 h 11 m 50 sec
Reputation Power: 0
|
|
Okay thanks for the responses. But yes this is in C++ and I want to use pointers. Also , sorry for the smilies and the lack of including my source code into the code brackets.
I worked on it a bit more and I think what I got looks good, but when I run it, it crashes! .exe has stopped working...blah blah. I've updated my strcat function so take a look maybe you guys can spot what's wrong?
Code:
#include<iostream>
using std::cin;
using std::cout;
const int SIZE_1 = 101;
const int SIZE_2 = 202;
void GetStrings(char * src, char * dest);
int MyStrLen(char * src, int & length);
void MyStrCat (int length, char * src, char * dest);
int main()
{
char string1[SIZE_1];
char * src = string1;
char string2[SIZE_1];
char * dest = string2;
int length = 0;
GetStrings(string1, string2);
MyStrLen(string1, length);
MyStrCat(length, src, dest);
return 0;
}
void GetStrings(char * src, char * dest)
{
cout << "Enter first Cstring (100 characters max): ";
cin >> src;
cout << "Enter second Cstring (100 characters max): ";
cin>> dest;
}
int MyStrLen(char * src, int & length)
{
int i = 0;
while (src[i] != '\0')
{
i++;
length++;
}
return length;
}
void MyStrCat (int length, char * src, char * dest)
{
while(*dest !='/0')
dest++;
while(*dest++=*src++);
}
|

October 2nd, 2012, 02:49 PM
|
 |
Contributing User
|
|
|
|
|
Let's turn on our compiler warnings and read the messages:
$ g++ -Wall -g -O0 -c C.C
...
C.C:58:17: warning: multi-character character constant [-Wmultichar]
C.C: In function ‘void MyStrCat(int, char*, char*)’:
...
Now we look at line 58.
'\0' or '/0'
I choose to avoid the issue.
while(*dest) dest++;
|

October 2nd, 2012, 03:07 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 5
Time spent in forums: 2 h 11 m 50 sec
Reputation Power: 0
|
|
b49P23TIvg, I appreciate your response .
I fixed the null character error. still no luck though.
to see the final concatenated string I would output dest right?
here's just my strcat function...It should work, I don't know what's wrong. it either outputs a space or 2 small ascii characters that are just symbols.
Code:
void MyStrCat(char * dest, const char * src) { //advance dest until null is found while(*dest !='\0') dest++; while (*dest++ = *src++); cout << *dest; }
|

October 2nd, 2012, 03:20 PM
|
 |
Contributing User
|
|
|
|
Now let's think about where dest points at the moment you decide to
cout << *dest;
also we might consider the data type of
*dest
Is it what you expect?
Code:
spoiler
(answers, possibly wrong: dest points one character beyond anything you deliberately changed, and *dest is a character so you shouldn't expect to see the whole string in general.)
Last edited by b49P23TIvg : October 2nd, 2012 at 03:23 PM.
|

October 2nd, 2012, 03:31 PM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 5
Time spent in forums: 2 h 11 m 50 sec
Reputation Power: 0
|
|
I thought about everyone's advice a little more, tweeked a few things and finally got my program working!
Also, I realize I shouldn't input my strings through my main function but I was just being lazy. Just glad I got the damn thing to work. those pointers are tricky little buggers.
here's my finished source code:
Code:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
void mystrcat(const char * source, char * target);
int main()
{
char target[100];
char source[100];
cout << "Enter first Cstring (100 characters max): ";
cin >> target;
cout << "Enter second Cstring (100 characters max): ";
cin>> source;
mystrcat(source,target);
cout << "Your concatenated string is: " << target << endl;
return 0;
}
void mystrcat(const char * source, char * target)
{
while(*target !='\0')
target++;
while(*source)
{
*target=*source;
source++;
target++;
}
*target = '\0'; //add null character to terminate final string.
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|