13 Replies - 1727 Views - Last Post: 13 March 2010 - 11:38 PM Rate Topic: -----

#1 jtharrill   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-February 10

String Modification Not using <cstring> Weird Output error.

Posted 13 March 2010 - 09:13 PM

I'm trying to do a homework assignment which requires that I alter 2 strings(Not using <cstring> or it's functions) in the following way. I've finished the code yet I get some weird TTW in 2 of my functions.
Any ideas why???

The first version should use array subscripting. The second version should use pointers and pointer arithmetic.

int stringLength(const char* s);

Determines the length of string s. The number of characters preceding the terminating null character is returned.

char* stringCopy(char* s1, const char* s2);

Copies the string s2 into the character array s1. The value of s1 is returned.

char* stringConcatenate(char* s1, const char* s2);

Appends a copy of string s2 to string s1. The first character of s2 overwrites the terminating null character of s1. The value of s1 is returned.





#include <iostream>
#include <iomanip>
using namespace std;

int stringLength1(char* s)
{
        int count = 0;
    do
    {
        if(s[count] != '\0')
        {
        count++;
        }
        
    }
    while(s[count] != '\0');
    
    return count;    
}
int stringLength2(char* s)
{
    
    char *sPtr = s;
    int count = 0;
    do
    {
     if(*sPtr != '\0')
        {
        
        count++;
        sPtr++;
        }
        else
        {
        break;
        }
    }
    while(*sPtr != '\0');
    
    return count; 


}
char* stringCopy1(char* s1, char* s2)
{
    
    int count = 0;
    do
    {
    if(s2[count] != '\0')
        {
        s1[count] = s2[count];
        count++;
        }
        
    }
    while(s2[count] != '\0');


return s1;
}char* stringCopy2(char *s1, char *s2)
{
      
*s1 = *s2;
int count = 1;
do
{
*(s1 + count) = *(s2 + count);
count++;   
}
while( *(s1 + count) != '\0');


return s1;
}
char* stringCat1(char* s1,  char* s2)
{
      int count1 = 0;
      char letter;
    
      //Counts to where to insert s2.
      int count = 0;
      do
      {
         if(s1[count] != '\0')
         {
         count++;
         }
         else;
        
      }
      while(s1[count] != '\0');    
      
      //Inserts S2 onto the end of S1.
      do
      {
              if(s2[count1] != '\0')
              {
              letter = s2[count1];
              s1[count] = letter;
              count1++;
              count++;
              }
     
    }
    while(s2[count1] != '\0');
    
    return s1;
}
char* stringCat2(char* s1,  char* s2)
{
int count2 = 0;
int count = 0;
char *sPtr = s1;
    
//Counts to where to insert s2.
    do
    {
     if(*sPtr != '\0')
        {
        
        count++;
        sPtr++;
        }
       
    }
    while(*sPtr != '\0');
  
//Add S2 to end of S1

//*s1 = *s2;

do
{
*(s1 + count) = *(s2 + count2);
count++; 
count2++;  
}
while( *(s2 + count2) != '\0');

return s1;
}



int main()

{
    
// Implement the following function prototypes
int stringLength1(char *s );
int stringLength2(char *s );
char *stringCopy1( char *s1, char *s2 );
char *stringCopy2( char *s1, char *s2 );
char *stringCat1( char *s1, char *s2 );
char *stringCat2( char *s1, char *s2 );


    
    
char s1[ 100 ];
   char* s2 = "education";
   char* s3 = "school";
  
   // Test stringLength functions
    cout << "stringLength(" << s2 << "): "
    << stringLength1( s2 ) << endl;
    cout << "stringLength(" << s3 << "): "
    << stringLength2( s3 ) << endl;

   // Test stringCopy functions
    cout << "stringCopy1(s1, " << s2 << "): "
    << stringCopy1( s1, s2 ) << endl;
    cout << "stringCopy2(s1, " << s3 << "): "
    << stringCopy2( s1, s3 ) << endl;
 
   // Test stringCat functions
    cout << "stringCat1(s1, " << s2 << "): "
    << stringCat1( s1, s2 ) << endl;
    cout << "stringCat2(s1, " << s3 << "): "
    << stringCat2( s1, s3 ) << endl;
 
   system("pause");
   return 0; // indicates successful termination
} // end main



     



Is This A Good Question/Topic? 0
  • +

Replies To: String Modification Not using <cstring> Weird Output error.

#2 taiku   User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 92
  • Joined: 28-October 09

Re: String Modification Not using <cstring> Weird Output error.

Posted 13 March 2010 - 09:29 PM

After some poking and prodding I have found the bug!!!

It's a simple slip up! When you append the strings using copy, you copy up until the second strings NULL terminator, but you dont actually copy it!

EDIT: actually, you dont seem to be doing this is any of your functions!! add it to all of them!

and also!!!!!
you should ALWAYS initialize all variables no matter what! so when you make your array of s1 you should do:
char s1[ 100 ] = {""};


This post has been edited by taiku: 13 March 2010 - 09:31 PM

Was This Post Helpful? 1
  • +
  • -

#3 jtharrill   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-February 10

Re: String Modification Not using <cstring> Weird Output error.

Posted 13 March 2010 - 09:33 PM

How do I copy the null terminator and also end the loop?
Was This Post Helpful? 0
  • +
  • -

#4 taiku   User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 92
  • Joined: 28-October 09

Re: String Modification Not using <cstring> Weird Output error.

Posted 13 March 2010 - 09:36 PM

Doesn't matter I was wrong, I tried it and it doesnt fix diddly squat... hmmmmm gimme like 2 minutes.
Was This Post Helpful? 1
  • +
  • -

#5 jtharrill   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-February 10

Re: String Modification Not using <cstring> Weird Output error.

Posted 13 March 2010 - 09:38 PM

Do you also get the TTW? I tried adding it after the loop with s1[count] = '\0'; But i dont think that worked either. Its due at midnight, but I'd still like to know why I'm getting this error.
Was This Post Helpful? 0
  • +
  • -

#6 taiku   User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 92
  • Joined: 28-October 09

Re: String Modification Not using <cstring> Weird Output error.

Posted 13 March 2010 - 09:39 PM

HAHA I WAS WRONG IM JUST RETARDED!!!

Ya, it does work, you could so something as simple as this:
char* stringCopy1(char* s1, char* s2)
{
	int count = 0;
	do
	{
	if(s2[count] != '\0')
        {
        	s1[count] = s2[count];
        	count++;
        }
        
    } while(s2[count] != '\0');

	s1[count] = '\0';
	return s1;
}



I have no idea what you mean by TTW to be honest, I get a memory overflow around s1...
Was This Post Helpful? 1
  • +
  • -

#7 jtharrill   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-February 10

Re: String Modification Not using <cstring> Weird Output error.

Posted 13 March 2010 - 09:47 PM

Ah, thanks I must have put \n by accident. I must be tired.
Was This Post Helpful? 0
  • +
  • -

#8 taiku   User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 92
  • Joined: 28-October 09

Re: String Modification Not using <cstring> Weird Output error.

Posted 13 March 2010 - 09:48 PM

K, glad to have helped, bud!

btw what is a TTW??? I can't find any sort of definition or anything!

and please give rep too! =D
Was This Post Helpful? 1
  • +
  • -

#9 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: String Modification Not using <cstring> Weird Output error.

Posted 13 March 2010 - 09:50 PM

Okay but your string concatenations were still wrong.

Why not reuse a little code by calling the stringLength1 and 2 functions instead of calculating string length every time.

I fixed the code with a couple tweaks here and there, mainly null-terminating all the strings. That is the reason why there was an overflow. cout doesn't know when to stop if it doesn't see the null-terminating character.

#include <iostream>
#include <iomanip>
using namespace std;

// Implement the following function prototypes
int stringLength1(char *s );
int stringLength2(char *s );
char *stringCopy1( char *s1, char *s2 );
char *stringCopy2( char *s1, char *s2 );
char *stringCat1( char *s1, char *s2 );
char *stringCat2( char *s1, char *s2 );

int stringLength1(char* s)
{
	int count = 0;
	do
	{
		if(s[count] != '\0')
		{
			count++;
		}

	}
	while(s[count] != '\0');

	return count;
}
int stringLength2(char* s)
{

	char *sPtr = s;
	int count = 0;
	do
	{
		if(*sPtr != '\0')
		{

			count++;
			sPtr++;
		}
		else
		{
			break;
		}
	}
	while(*sPtr != '\0');

	return count;


}
char* stringCopy1(char* s1, char* s2)
{

	int count = 0;
	do
	{
		if(s2[count] != '\0')
		{
			s1[count] = s2[count];
			count++;
		}

	}
	while(s2[count] != '\0');

	s1[count] = '\0';
	return s1;
}
char* stringCopy2(char *s1, char *s2)
{

	*s1 = *s2;
	int count = 1;
	do
	{
		*(s1 + count) = *(s2 + count);
		count++;
	}
	while( *(s1 + count) != '\0');

	s1[count] = '\0';
	return s1;
}
char* stringCat1(char* s1,  char* s2)
{
	int count1 = 0;
	char letter;

	/*
	//Counts to where to insert s2.
	int count = 0;
	do
	{
		if(s1[count] != '\0')
		{
			count++;
		}
		else;

	}
	while(s1[count] != '\0');
	*/

	int	count = stringLength1(s1);
	//Inserts S2 onto the end of S1.
	do
	{
		if(s2[count1] != '\0')
		{
			letter = s2[count1];
			s1[count] = letter;
			count1++;
			count++;
		}

	}
	while(s2[count1] != '\0');

	s1[count] = '\0';
	return s1;
}
char* stringCat2(char* s1,  char* s2)
{
	int count2 = 0;
	int count = 0;
	char *sPtr = s1;

	/*
	//Counts to where to insert s2.
	do
	{
		if(*sPtr != '\0')
		{

			count++;
			sPtr++;
		}

	}
	while(*sPtr != '\0');
	*/
	count = stringLength2(s1);

//Add S2 to end of S1

//*s1 = *s2;

	do
	{
		*(s1 + count) = *(s2 + count2);
		count++;
		count2++;
	}
	while( *(s2 + count2) != '\0');

	s1[count] = '\0';
	return s1;
}



int main()
{
	char s1[ 100 ];
	char* s2 = "education";
	char* s3 = "school";

	// Test stringLength functions
	cout << "stringLength(" << s2 << "): "
	     << stringLength1( s2 ) << endl;
	cout << "stringLength(" << s3 << "): "
	     << stringLength2( s3 ) << endl;

	// Test stringCopy functions
	cout << "stringCopy1(s1, " << s2 << "): "
	     << stringCopy1( s1, s2 ) << endl;
	cout << "stringCopy2(s1, " << s3 << "): "
	     << stringCopy2( s1, s3 ) << endl;

	// Test stringCat functions
	cout << "stringCat1(s1, " << s2 << "): "
	     << stringCat1( s1, s2 ) << endl;
	cout << "stringCat2(s1, " << s3 << "): "
	     << stringCat2( s1, s3 ) << endl;

	system("pause");
	return 0; // indicates successful termination
} // end main


Was This Post Helpful? 1
  • +
  • -

#10 taiku   User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 92
  • Joined: 28-October 09

Re: String Modification Not using <cstring> Weird Output error.

Posted 13 March 2010 - 09:53 PM

Way to steal my thunder...
Was This Post Helpful? 1
  • +
  • -

#11 jtharrill   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-February 10

Re: String Modification Not using <cstring> Weird Output error.

Posted 13 March 2010 - 10:09 PM

I added null terminators to each string, but didn't get a chance to do anything futher before turning it in. I didn't think to call the count function though, I just copied and pasted the actual calculation. But the main function was actually given so I didn't alter it.

Is rep just saying was this post helpful?
Was This Post Helpful? 0
  • +
  • -

#12 eker676   User is offline

  • Software Engineer
  • member icon

Reputation: 379
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: String Modification Not using <cstring> Weird Output error.

Posted 13 March 2010 - 10:12 PM

Quote

Is rep just saying was this post helpful?


I'm assuming you noticed the question directly to the left of the plus/minus button? ;)

Reputation - A Primer

This post has been edited by eker676: 13 March 2010 - 10:14 PM

Was This Post Helpful? 1
  • +
  • -

#13 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 555
  • View blog
  • Posts: 1,436
  • Joined: 22-August 09

Re: String Modification Not using <cstring> Weird Output error.

Posted 13 March 2010 - 10:45 PM

I hate typing!

#include <iostream>
#include <iomanip>
using namespace std;

int stringLength1(char* s) {
	int count;
	for ( count = 0; s[count] != 0; count++ );
    return count;    
}

int stringLength2(char* s) {
	int count = 0;
	while ( *s++ != 0 ) count++;
    return count; 
}

char* stringCopy1(char* s1, char* s2) {
    int count;
	for ( count = 0; s2[count] != 0; s1[count] = s2[count], count++ );
	s1[count] = 0;
    return s1;
}

char* stringCopy2(char *s1, char *s2) {     
 	while ( *s2 != 0 ) *s1++ = *s2++;
	*s1 = 0;
    return s1;
}

char* stringCat1(char* s1,  char* s2) {
	stringCopy1(&s1[stringLength1(s1)], s2);
    return s1;
}

char* stringCat2(char* s1,  char* s2) {
	stringCopy2(&s1[stringLength2(s1)], s2);
    return s1;
}

int main() {

	// Implement the following function prototypes
	int stringLength1(char *s );
	int stringLength2(char *s );
	char *stringCopy1( char *s1, char *s2 );
	char *stringCopy2( char *s1, char *s2 );
	char *stringCat1( char *s1, char *s2 );
	char *stringCat2( char *s1, char *s2 );

	char s1[ 100 ];
	char* s2 = "education";
	char* s3 = "school";
  
	// Test stringLength functions
    cout << "stringLength(" << s2 << "): " << stringLength1( s2 ) << endl;
    cout << "stringLength(" << s3 << "): " << stringLength2( s3 ) << endl;

	// Test stringCopy functions
    cout << "stringCopy1(s1, " << s2 << "): " << stringCopy1( s1, s2 ) << endl;
    cout << "stringCopy2(s1, " << s3 << "): " << stringCopy2( s1, s3 ) << endl;
 
	// Test stringCat functions
    cout << "stringCat1(s1, " << s2 << "): " << stringCat1( s1, s2 ) << endl;
    cout << "stringCat2(s1, " << s3 << "): " << stringCat2( s1, s3 ) << endl;
 
   return 0; // indicates successful termination
} // end main


Was This Post Helpful? 1
  • +
  • -

#14 jh4life   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 25-April 08

Re: String Modification Not using <cstring> Weird Output error.

Posted 13 March 2010 - 11:38 PM

Sorry, wrong topic.

This post has been edited by jh4life: 13 March 2010 - 11:45 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1