C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 2nd, 2012, 01:13 PM
SkinnyBones92 SkinnyBones92 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 5 SkinnyBones92 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!!

}

Reply With Quote
  #2  
Old October 2nd, 2012, 01:50 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
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!

Reply With Quote
  #3  
Old October 2nd, 2012, 01:58 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,255 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 20 h 26 m 55 sec
Reputation Power: 1985
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.

Reply With Quote
  #4  
Old October 2nd, 2012, 02:32 PM
SkinnyBones92 SkinnyBones92 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 5 SkinnyBones92 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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++);

}

Reply With Quote
  #5  
Old October 2nd, 2012, 02:49 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
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++;

Reply With Quote
  #6  
Old October 2nd, 2012, 03:07 PM
SkinnyBones92 SkinnyBones92 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 5 SkinnyBones92 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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; }

Reply With Quote
  #7  
Old October 2nd, 2012, 03:20 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,458 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 26 m 43 sec
Reputation Power: 403
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.

Reply With Quote
  #8  
Old October 2nd, 2012, 03:31 PM
SkinnyBones92 SkinnyBones92 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 5 SkinnyBones92 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Trying to write my own Strcat function

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap