#include<iostream>
using std::cin;
using std::cout;
using std::endl;
void switchthese (char[], char[]);
int main()
{
char first[] = "sssssss";
char second[] = "aaa";
cout<<first<<endl;
cout<<second<<endl;
switchthese(first,second);
cout<<first<<endl;
cout<<second<<endl;
}
void switchthese(char first[], char second[])
{
int i = 0;
while(first[i]!='\0' && second[i]!='\0')
{
char temp=first[i];
first[i]=second[i];
second[i]=temp;
i++;
}
}
If you need some clarification on something, please ask. Thank you all! I truly have tried to look around the forums for DAYS, but I cannot use pointers, or strlen and strcpy to complete this problem.
okay, the thing is I have solved the problem using a different method, but I cannot use this code in this case (I cannot use another function within a function and I did this to find the length of both arrays, can only use one loop in the swap function, and I can only use a null terminator or some other sentinel to stop the loop).
I was wondering if someone knows a way to write the working code below with these restrictions...The first post was my attempt at rewriting the code below. Thanks!
okay, the thing is I have solved the problem using a different method,
No, you haven't. What you've done is invoke undefined behavior using a different method. Again, the array that is smaller than the other cannot hold the same number of elements the larger array holds.
#include <iostream>
void swap(char& a, char& b)
{
char c = a;
a = b;
b = c;
}
// assumes both a and b point to enough memory to hold the
// longest of the strings.
void swap_strings(char* a, char* b)
{
unsigned i = 0, j = 0;
unsigned eos_count = 0; // end-of-string count.
while (eos_count < 2)
{
swap(a[i], b[j]);
if (!a[i])
++eos_count;
if (!b[j])
++eos_count;
++i, ++j;
}
}
int main()
{
char one[32] = "one";
char three[32] = "three";
std::cout << "one: \"" << one << "\"\n";
std::cout << "three: \"" << three << "\"\n";
swap_strings(one, three);
std::cout << "one: \"" << one << "\"\n";
std::cout << "three: \"" << three << "\"\n";
}
Is there a way to write that function without ampersands and pointers? The code looks great and it works, but I cannot use pointers and ampersands for this problem.
Also... I compiled the code from my second post without the length statements (they were remnants of a code that is no longer used):
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
void switchthese (char[], char[]);
int main()
{
char first[] = "superb";
char second[] = "excellent";
cout<<first<<endl;
cout<<second<<endl;
switchthese(first,second);
cout<<first<<endl;
cout<<second<<endl;
}
void switchthese(char first[],char second[])
{
int i = 0;
while(true)
{
char temp=first[i];
first[i]=second[i];
second[i]=temp;
i++;
if(first[i]=='\0' && second[i]=='\0')break;
}
}
can anyone confirm if the code above abides by the following:
1) no other loops were used other than the while loop in the void function (if-break statements are not loops as far as I know).
2) I used a null terminator to end the loop (in this case '\0' which is the last character of all arrays)
also, I find it odd that the code compiles on compileonline.com, but it gives me an error in Visual Studio 2010...
Is there a way to write that function without ampersands and pointers? The code looks great and it works, but I cannot use pointers and ampersands for this problem.
There is no way to write a "swap" function without using pointers or references, no, but if you want to avoid the use of an ampersand or asterisk, that is relatively easy.
void switchthese(char* first, char* second)
is entirely equivalent to:
void switchthese(char first[], char second[])
first and second are pointers regardless of the notation used.
Your newest code still has the same problem all of your previous code has had. One of the arrays is not large enough to hold all of the elements in the other.
#include <iostream>
// assumes both a and b point to enough memory to hold the
// longest of the strings.
void swap_strings(char a[], char b[])
{
unsigned i = 0, j = 0;
unsigned eos_count = 0; // end-of-string count.
while (eos_count < 2)
{
char c = a[i] ;
a[i] = b[j] ;
b[j] = c ;
if (!a[i])
++eos_count;
if (!b[j])
++eos_count;
++i, ++j;
}
}
int main()
{
char one[32] = "one"; // Notice the size of the arrayschar three[32] = "three";
std::cout << "one: \"" << one << "\"\n";
std::cout << "three: \"" << three << "\"\n";
swap_strings(one, three);
std::cout << "one: \"" << one << "\"\n";
std::cout << "three: \"" << three << "\"\n";
}