-1

Basically, my goal for the struggling part is to swap the first letters of each string and print it. Everything seems to work except my code will not print B. I realize I need to store A[0] into a temp variable so it is not overwritten before being stored in B[0]. For some reason when I run my code (in the sites compiler), it will just print "ebcd". It never prints B even though I am telling it to.

#include <iostream>
#include <string>
#include <bits/stdc++.h>
using namespace std;

void wordSize(string a, string b){
    int lenA = a.size();
    int lenB = b.size();

    cout << lenA << " " << lenB << endl;
}

void firstLetterSwap(string a, string b){
    int sizeA = a.size();
    int sizeB = b.size();
    char temp;
    char* A = new char[sizeA];
    char* B = new char[sizeB];
    strcpy(A, a.c_str());
    strcpy(B, b.c_str());

    A[0] = temp;
    A[0] = B[0];
    B[0] = temp;

    cout << A << " " << B << endl;
}

int main() {
    string a, b;
    cin >> a; 
    cin >> b;

    wordSize(a, b);
    cout << a + b << endl;
    firstLetterSwap(a, b);

    return 0;
}
Neeraj Sewani
  • 2,662
  • 4
  • 26
  • 41
Fall0ut
  • 75
  • 11
  • 8
    `#include ` - don't do this, it's not clever and it's not portable. And why do you feel the need to mix C-style strings and std::string, or to allocate anything explicitly dynamically? –  Apr 06 '18 at 19:14
  • 1
    Supporting information for comment by @NeilButterworth. https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h. – R Sahu Apr 06 '18 at 19:17
  • All of your code in `firstLetterSwap` is unnecessary. All you need `std::swap(a[0], b[0]);` – NathanOliver Apr 06 '18 at 19:17
  • Every `new` requires a `delete`. So don't use `new`, and you can't forget `delete'. – Jive Dadson Apr 06 '18 at 19:24
  • I warmly recommend you to read some C or C++ basics book, preferably C, before you continue further. – BJovke Apr 06 '18 at 19:32
  • What is the value of `temp` when you set `A[0] = temp;`? (Boom! Undefined Behavior) – David C. Rankin Apr 06 '18 at 19:42
  • Thank you for all the tips aside from corrections. Yeah I’m only a student in college and my old school taught java that’s why I’m practicing off of the site. Have a bit of catching up to do. – Fall0ut Apr 06 '18 at 20:04

3 Answers3

2

You must pass the strings by reference, else the changes will not be seen by the caller. As for swapping, there's a function for that.

void firstLetterSwap( std::string& a, std::string& b){
    std::swap(a[0], b[0];
    // and that's all, folks
}
Jive Dadson
  • 15,176
  • 9
  • 47
  • 65
1

You could use std::swap from <algorithm>

#include <iostream>     //cout
#include <string>
#include <algorithm>    //swap()

using namespace std;

//passing strings by reference to work directly with them
void firstLetterSwap(string &a, string &b){
    swap(a[0], b[0]);
}

int main() {
    string a = "first";
    string b = "second";

    firstLetterSwap(a, b);
    cout << a + b << endl;

    return 0;
}

Output:

sirstfecond
ivanjermakov
  • 743
  • 6
  • 16
0

I see the following problems in your implementation of firstLetterSwap.

  1. You are getting the arguments by value. No matter what you do to them in the function, the variables in the calling functions are not affected. If you want the changes made to the variables in the function to be visible in the calling function, you will need to pass them by reference.

  2. You are not making any changes to the arguments in the function. You are making copies of the input arguments and making changes to the copies.

  3. You are not allocating enough space to A and B before calling strcpy. std::string::size returns a value that excludes the terminating null character. Hence, new char[sizeA] will allocate memory that can hold one less character than what you need. Consequently, your program has undefined behavior.

  4. Dynamically allocated memory is not deallocated. Every call to new/new [] should be accompanied by a corresponding delete/delete [].


The simplest fix is to use:

void firstLetterSwap(std::string& a, std::string& b)
{
    std::swap(a[0], b[0];
}
R Sahu
  • 196,807
  • 13
  • 136
  • 247