im trying to stick two strings together without using + operator,also using loops to to that.the problem is after when it read two strings it couldnt print the second string and only the first string appears.
here is my code
this code is like copying two strings in one.
char str1[MAX];
char str2[MAX];
cout<<"Enter The first String:\n";
cin.getline(str1,MAX,'\n');
cout<<"Enter the second String:\n";
cin.getline(str2,MAX,'\n');
char str3[2*MAX]; int k=0;
for(int i=0;i<MAX;i++)
{ str3[k]=str1[i]; k++; }
for(int j=0;j<MAX;j++)
{ str3[k]=str2[j]; k++; }
str3[k]='\0';
cout<<endl<<"Here is the concatenated string:\n";
cout<<str3<<endl;
It is better to write such code using pointers.
So I would substitute this wrong code
char str3[2*MAX]; int k=0;
for(int i=0;i<MAX;i++)
{ str3[k]=str1[i]; k++; }
for(int j=0;j<MAX;j++)
{ str3[k]=str2[j]; k++; }
str3[k]='\0';
for the following
char str3[2 * MAX];
char *p = str3;
char *q = str1;
while ( *p = *q++ ) ++p;
q = str2;
while ( *p++ = *q++ );
Also the same can be written using for loops. For example
char str3[2 * MAX];
char *p = str3;
for ( char *q = str1; *p = *q++; ++p );
for ( char *q = str2; *p++ = *q++; );
Your code goes past the end of str1 and str2, including their null terminators. Once the null terminator of srt1 is copied, C string inside str3 is considered complete, so str2 part is ignored.
You need to modify the first loop to stop once it sees '\0' in str1, and copy str2 from that point on. Do the same for the second loop. Your code adds null termination already, so the result will be correct:
for(int i=0;i<MAX && str1[i] != '\0';i++)
{ str3[k]=str1[i]; k++; }
for(int j=0;j<MAX && str2[j] != '\0';j++)
{ str3[k]=str2[j]; k++; }
Note: I am assuming that this is a learning exercise for which you are not allowed to use std::string.
Correct code should be:-
char str3[2*MAX];
int k=0;
for(int i = 0; str[i] != '\0'; i++)
{
str3[k]=str1[i];
k++;
}
for(int j=0 ; str2[j] != '\0'; j++ )
{
str3[k] = str2[j];
k++;
}
str3[k]='\0';
You were not taking into account null terminator and hence reading past that.
Change this :
for(int i=0; str1[i] ;i++)
{ str3[k]=str1[i]; k++; }
for(int j=0; str2[j] ;j++)
{ str3[k]=str2[j]; k++; }
You have to stop concatenating when str1 or str2 ends that is when str1[i] or str2[j]
will be 0 ('\0'). But you were looping through MAX. That's why your program produced wrong output.
Hope you understand now :)
#dasblinkenlight explains why this doesn't work in her/his answer.
Here's yet another solution using the standard C(++) library function strcat defined in . See http://www.cplusplus.com/reference/cstring/strcat/
#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char** argv) {
char str1[MAX];
char str2[MAX];
cout << "Enter The first String:" << endl;
cin.getline(str1, MAX);
cout << "Enter the second String:" << endl;
cin.getline(str2, MAX);
char str3[2 * MAX];
strcat(str3, str1);
strcat(str3, str2);
cout << endl << "Here is the concatenated string:" << endl
<< str3 << endl;
}
Related
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
cout << "Enter a string: ";
getline(cin, str);
int length = str.length();
string temp;
int k = 0;
for(int i = length-1; i>=0; i--){
temp[++k] = str[i];
}
cout<<temp;
return 0;
}
Actually, i want to find out whether the given string is a palindrome or not, so i am storing the first string in second one in reverse order , and then will ultimately check whether they are equal or not, but i am unable to print out even the result of temp string
You defined an empty object of the type std::string
string temp;
So you may not use the subscript operator with the empty object in the loop.
int k = 0;
for(int i = length-1; i>=0; i--){
temp[++k] = str[i];
}
Using your approach you could just write
string temp( str.rbegin(), str.rend() );
without using a loop.
However to check whether a string is a palindrome there is no need to create an intermediate string.
You could do it in a loop the following way.
std::string::size_type i = 0;
for ( auto n = str.length(); i < n / 2 && str[i] == str[n - i - 1]; )
{
++i;
}
if ( i == str.length() /2 ) std::cout << str << " is a palindrome\n";
Or without the loop and defining one more variable you could write
if ( str == std::string( str.rbegin(), str.rend() ) )
{
std::cout << str << " is a palindrome\n";
}
I am trying to learn some of this beautiful language but I've got stuck on this. Problem is: Why does the last count shows only Witaj PJC not Witaj Cpp PJC? As you see function app has to append transformed 2nd word to 1st one.
Thanks for any help.
If you could give me any good tutorial about pointers I would appreciate that. Thanks!
#include <iostream>
#include <string.h>
using namespace std;
void app(char *str2, char *str1){
for(int i =0; i < strlen(str2); i++){
*(str2++);
}
for(int i =0; i < strlen(str1); i++){
*(str1++);
}
for(int i =0; i < strlen(str1); i++){
*(str2)=*(str1);
*(str2)++;
*(str1)--;
}
}
int main()
{
char *str1 = "ppC ";
char str2[20] = "Witaj";
cout << str2 << endl; // Witaj
app(str2, str1);
cout << str2 << endl; // Witaj Cpp shows WitCpp
app(str2, "CJP ");
cout << str2 << endl; // Witaj Cpp PJC shows WitPJ
return 0;
}
Your problem is this sort of loops:
for(int i =0; i < strlen(str2); i++){
*(str2++);
}
You can't move your pointer with str2++ and expect that strlen(str2) still returning the lenght of the original one.
For loop variables, in each iteration:
i str2 strlen(str2) condition
Iteration 1 0 Witaj 5 0 < 5 Ok
Iteration 2 1 itaj 4 1 < 4 Ok
Iteration 3 2 taj 3 2 < 3 Ok
Iteration 4 3 aj 2 3 < 2 Exit at 3rd character!!
Thus.. you only "move" your pointer 3 bytes.
Change your app function for that one:
void app(char *str2, char *str1){
int nstr2 = strlen(str2);
int nstr1 = strlen(str1);
for(int i =0; i < nstr2; i++){
*(str2++);
}
for(int i =0; i < nstr1; i++){
*(str1++);
}
for(int i =0; i < nstr1; i++){
*(str2++)=*(--str1);
}
}
Anyway... this program is only for academic porpouses or you are thinking use it professionally?
And for some functioning code for just string appending, i scribbled this...
Note that you should make a const call instead, and if you want to reverse one of the strings (a bit unclear from your question) it should be done prior to appending.
Example of string append (rather unsafely and rudimentary) using a new allocation:
char* app(char *str2, char *str1){
char* appendedstring = (char*)malloc(sizeof(char)*20);
char *temp = str1;
char *temp2 = str2;
int stringlen1 = strlen(str1);
int stringlen2 = strlen(str2);
//Copy string 1
for (int i = 0; i < stringlen2; i++){
appendedstring[i] = *temp2;
temp2++;
}
//Append string 2
for (int i = 0; i < stringlen1 + 1; i++){
appendedstring[stringlen2 + i] = *temp;
temp++;
}
return appendedstring;
}
int main()
{
int t;
char *str1 = "ppC ";
char str2[20] = "Witaj";
cout << str1 << endl;
cout << str2 << endl; // Witaj
char* newstr = app(str2, str1);
cout << newstr << endl; // Witaj Cpp shows WitCpp
char* newstr2 = app(str2, "CJP ");
cout << newstr2 << endl; // Witaj Cpp PJC shows WitPJ
return 0;
}
I have a program to find and replace words in C++.
#include<iostream.h>
#include<string.h>
int main()
{
char string[80], replace[80], found[80], str1[80], str2[80], str3[80];
cout << "\nEnter string(max 3 words)\n";
cin.getline(string , 80);
cout << "\nEnter the word to be Found\n";
cin.getline(found , 80);
cout << "\nReplace with \n";
cin.getline(replace , 80);
for(int i = 0; string[i] != '\0'; i++)
{
str1[i] = string[i];
if(str1[i] == " ")
break;
}
for(int j = i; string[j] != '\0'; j++)
{
str2[j] = string[j];
if(str2[j] == " ")
break;
}
for(int k = j; string[k] != '\0'; k++)
{
str3[k] = string[k];
if(str3[k] == " ")
break;
}
cout << str1;
cout << str2;
cout << str3;
}
For this I stored every word as a different string, but it doesn't help.
What should be done to improve this?
Your code has too many logical & syntactical error.
Here is the modified code which will accept the required string and print expected output:
#include<iostream>
#include<string.h>
using namespace std;
void strreplace(string orgString, const string search, const string replace )
{
for( size_t pos = 0; ; pos += replace.length() )
{
pos = orgString.find( search, pos );
if( pos == string::npos )
break;
orgString.erase( pos, search.length() );
orgString.insert( pos, replace);
cout<<"String after replacement:"<<orgString<<endl;
}
}
int main()
{
char string[80], replace[80], found[80], str1[80], str2[80], str3[80] ;
cout << "\nEnter string(max 3 words)\n" ;
cin.getline(string , 80);
cout <<"\nEnter the word to be Found\n";
cin.getline(found , 80);
cout <<"\nReplace with \n" ;
cin.getline(replace , 80);
strreplace(string, found, replace);
return 0;
}
I hope this will help you.
In your current code you need to:
Use single quotes to compare characters for equality, not double quotes
Increment another index in your second and third loops. This is because the index for str2 and str3 needs to start at 0, not at the current position being looked at in string
Initalize the main string index (i) in second and third loops with (current value + 1) to skip past the space that it is currently at.
Null terminate your str1, str2, str3
1
if(str1[i] == " ")
should be
if(str1[i] == ' ')
2,3 Instead of
for(int j = i;string[j] != '\0' ; j++)
do
for (int j = 0, i = (i + 1); string[i] != '\0'; j++,i++)
The assignment becomes
str2[j] = string[i];
Do the same for the 3rd loop (without the int in front of j or use another letter). For consistency you could add a j variable starting at 0 to the first loop as well.
4 After each loop add an assignment statement for the null terminator (every c-string needs '\0' at the end to work properly) :
str1[i] = '\0';
str2[j] = '\0';
str3[j] = '\0';
std::string doesn't contain such function instead you could use stand-alone replace function from algorithm header.
#include <algorithm>
#include <string>
string stringReplace(std::string toSearch, std::string toReplace, std::string originalString) {
std::replace( originalString.begin(), originalString.end(), 'toSearch', 'toReplace'); // replace all 'toSearch' to 'toReplace'
return originalString;
}
The purpose of the following code was to create an strcat function using only basic array manipulation. A destination char array is input by a user and a source char array is appended to the end of it. My code works mostly fine except for the random chars it spits out for certain input char arrays. For example if I have my destination input as cheese and my source input as burger, the output is cheeseburger, as it should be. However if my destination input is dragon and my source input is fly, dragonfly should be the output. However, the output is given as dragonfly#. I have no idea what's wrong, and need help.
#include <iostream>
#include <string>
using namespace std;
void mystrcat ( char destination[], const char source[]);
int main(){
char source[80];
char destination[80];
cout << "Enter a word: ";
cin >> source;
cout << "\n";
cout << "Enter a second word: ";
cin >> destination;
mystrcat(destination, source);
}
void mystrcat ( char destination[], const char source[]){
int x=0;
for(int i=0; destination[i] != '\0'; i++)
{
if ( destination[i] != '\0')
{
x = x + 1;
}
}
for(int i=0; source[i] != '\0'; i++)
{
destination[i + x] = source[i];
}
cout << destination << endl;
}
Basically, you just need to add a null-character ('\0') at the end of the destination array.
Here is the correct (and slightly simplified) implementation:
void mystrcat(char destination[], const char source[])
{
int x = 0;
while (destination[x] != '\0')
{
x++;
}
for (int i=0; source[i] != '\0'; i++)
{
destination[x++] = source[i];
}
destination[x] = '\0';
}
But you should be aware that you have no safety-assertion on the size of the destination array...
You don't terminate the destination string. You need to add the '\0' character at the end.
short code:
void _mystrcat_(
__in char * out,
__in char * in)
{
while (*out) out++;
do { *out++ = *in++; } while (*in);
*out = 0x0;
}
I am writing a simple program, where all the 'spaces' will be replaced by '%20'.
#include <iostream>
#include <string>
using namespace std;
int main (int argc, char* argv[]){
string input;
cout << "please enter the string where spaces will be replaced by '%20'" << endl;
getline(cin, input);
//count the number of spaces
int countSpaces = 0;
for (int i = 0 ; i < input.length() ; i++){
if (input[i] == ' '){
countSpaces++;
}
}
int size = input.length() + (2 * countSpaces) + 1;
//char cstr1[size];
char *cstr1 = new char[size];
char *cstr = cstr1;
for (int i = 0 ; i < input.length() ; i++){
if(input[i] == ' '){
*cstr++ = '%';
*cstr++ = '2';
*cstr++ = '0';
}
else{
*cstr++ = input[i];
}
}
*cstr == '\0';
cout << cstr1 << endl;
delete[] cstr1;
return 0;
}
I get the following strange behavior:
With the test input "this is strange " I get "this%20is%20strange%20%20his is" , where I just expect "this%20is%20strange%20%20"
If I hard code the same string, I get the correct results.
Replacing char *cstr1 = new char[size]; with char cstr1[size]; & removing the delete[] while still fetching the input via getline also removes the error.
I am using i686-apple-darwin10-g++-4.2.1:
Any help is much appreciated.
The last line must be *cstr = '\0'; not ==
Change the *cstr == '\0'; in the end of your code to *cstr = '\0';
ViolĂ !
*cstr == '\0';
this line checks if *cstr is equal to '\0' or not and return 1 or 0 accordingly
that is wrong as you want to insert the \0 character at the end of the string
so write single = instead of double =