2

In my code I am trying to add two strings together, however for some reason I can't seem to get the correct return type for my stringAdd function. I want to be able to return a c-string. And my implementation doesn't seem to work either. Any suggestions?

 #include <iostream>
 #include<cstring>

 using namespace std;
 int stringLength(char *); // Function prototype 
 char stringAdd(char *strPtr, char *strPtr2);//Function prototype

int main()
{
   const int SIZE = 51; // Array size 
   char letter; // The character to count 
   char word1[SIZE] = "Happy ";
   char word2[SIZE] = "Birthday";

   cout <<"Your first c-string is: "<<word1<<"Your second c-string is: "<<word2<<"\n";
   cout << "The length of your first c-string is: ";

   cout << stringLength(word1) << " chars long.\n";
   cout << "The length of your second c-string is: ";

   cout << stringLength(word2) << " chars long.\n";

   if (SIZE >= (stringLength(word1) + stringLength(word2) + 1))
   {
      cout << "we are gunna add ur strings";
      stringAdd(word1, word2);
   }
   else
   {
      cout << "String1 is not large enough for both strings.\n";
   }
   return 0;
}

int stringLength(char *strPtr)
{
    int times = 0; // Number of times a char appears in the string 

    // Step through the string each char. 
    while (*strPtr != '\0')
    {
        if (*strPtr != '0') // If the current character doesnt equals the null terminator... 
           times++; // Increments the counter 
        strPtr++; // Goes to the next char in the string. 
    }
    return times;
}

Up until this point my code works fine however the function below doesn't seem to work at all. I'm not sure how I can add two c-strings using reference

char stringAdd(char *strPtr, char *strPtr2)
{
   int size1;
   int size2;
   size1= stringLength(strPtr);
   int j=size1+1; // counter set to the num of chars in the first c-string
   int i = 0; // counter for to add to the 2nd c-string
   size2= stringLength(strPtr2);
   size1=+size2;
   char newWord[size1];

   for(int i=0;i<size1;i++)
      newWord[i] = *strPtr[i]
   for(int j=0;j<size2;j++)
      newWord[i]= *str
}
1
  • 2
    In your inner if (*strPtr != '0'), you forgot to escape the 0 – you probably meant '\0'. Commented Feb 1, 2016 at 12:29

4 Answers 4

4

Firstly, use std::string.

Then, use std::string.

Finally, if you really really must manipulate char arrays manually, then at least use the C standard library functions so you have a hope of getting null-termination right. The function you're looking for is std::strcat, which concatenates two strings.

After that, use std::string.

Sign up to request clarification or add additional context in comments.

Comments

0

You have a typo in stringAdd that causes a bug

size1=+size2;

This should be

size1 += size2;

Otherwise you're simply overwriting size1 with the value of size2. That being said, in C++ you aren't allowed to do this either

char newWord[size1];

The size of an array must be known at compile time not run time.

Comments

0

Function stringAdd is incorrect and has undefined behavior because it returns nothing.

Also this if statement in function stringLength

if (*strPtr != '0') // If the current character doesnt equals the null terminator... 

    times++; // Increments the counter 

doea not make great sense because the condition in the enclosing while statement

while (*strPtr != '\0')

is the same as in the if statement.

{

The functions can be written the following way

size_t stringLength( const char *strPtr )
{
    size_t n = 0;

    while ( strPtr[n] ) ++n;

    return n;
}


char * stringAdd( char *strPtr, const char *strPtr2 )
{
    char *p = strPtr + stringLength( strPtr );

    while ( *p++ = *strPtr2++ );

    return strPtr;
}

And in main you could write

if (SIZE >= (stringLength(word1) + stringLength(word2) + 1)) {
    cout << "we are gunna add ur strings" << endl;
    cout << stringAdd(word1, word2) << endl;
}
//...

In this case word2 would be appended to word1.

Comments

0

You have tagged this as "c++" and "c-strings", which is kind of like asking for a car that is powered by foot-power.

You have 2 options:

  1. Use C++ strings
  2. Use C strings

For the former:

#include <iostream>
#include <string>

int main()
{
    std::string word1 = "Happy";
    std::string word2 = "Birthday";
    // ... your other stuff
    std::string result = word1 + " " + word2 + "!";
    std::cout << "Result is " << result << std::endl;
    return 0;
}

For the latter:

#include <iostream> // if you are stuck using c-strings, this is kind of odd
#include <cstring>
#include <memory>

int main()
{
    const char* word1 = "Happy";
    const char* word2 = "Birthday";
    const unsigned int newWordSize = 20; // you only need 16 for this, so 20 is sufficient
    char newWord[newWordSize];
    std::memset(newWord, newWordSize, 0);
    std::strcpy(newWord, word1);
    std::strcat(newWord, " ");
    std::strcat(newWord, word2);
    std::strcat(newWord, "!");
    std::cout << "New Word is " << newWord << std::endl;
    return 0;
}

Why what you are doing is wrong:

// NOTE:  If your null-terminators are not set, this breaks as it is an infinite loop.
int stringLength(char *strPtr)
{
    // NOTE:  pointer arithmetic can speed up this function, and make this variable unnecessary
    int times = 0; // Number of times a char appears in the string 

    // Step through the string each char. 
    while (*strPtr != '\0')
    {
        if (*strPtr != '0') // If the current character doesnt equals the null terminator... 
           times++; // Increments the counter 
        strPtr++; // Goes to the next char in the string. 
    }
    return times;
}

char stringAdd(char *strPtr, char *strPtr2) // ERROR:  your return value should probably be char*, and you will need to free that memory later
{
   int size1;
   int size2;
   size1= stringLength(strPtr);
   int j=size1+1; // counter set to the num of chars in the first c-string
   int i = 0; // counter for to add to the 2nd c-string
   size2= stringLength(strPtr2);
   size1=+size2;
   char newWord[size1]; // ERROR:  you cannot allocate a dynamic array this way.

   for(int i=0;i<size1;i++) // ERROR:  you've set size1 = size1 + size2 + 1, and you attempt to access the first word with this new size.  You will access memory outside the bounds of your array
      newWord[i] = *strPtr[i]
   for(int j=0;j<size2;j++)
      newWord[i]= *str
    // ERROR:  You do not set the null-terminator for the new string
    // ERROR:  you do not return anything
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.