can any one elaborate the steps with comments

//Write a program to copy one string to another string, input the string into the
//first string variable and then copy this string to the second string variable by copying characters one by one.

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

main()
{
char str1[15], str2[15];

int i;
cout<<"Enter any string = ";

cin>>str1;

for(1=0,str1[i]!='\0';i++)

str2[i]='\0';

cout<<str2;

getch();

}
The code is heading in the right direction.
I would recommend cin>>str1; is changed to cin.getline(str1,15);

The for loop has several issues. There are two errors in this line, the compiler should issue error messages:
for(1=0,str1[i]!='\0';i++)

Also the body of the loop is either missing or incorrect.
chervil:
thanks, i want to ask you , does it works in DEV C++ soft, and can you help in correction of for Loop, compiler is giving error in Loop.
The C++ standard requires that headers were declared without extension .h and standard C headers were prefixed by letter 'c'.

#include <iostream>
#include <conio.h>
#include <cstdlib>

Function main shall have return type int:

int main()

It would be better to define some named constant for the arbitrary number 15

const int N = 15;
char str1[N], str2[N];

For entering string literals in a character array it is better to use getline function.

Either you shall specify nested name specifier std:: as

std::cout<<"Enter any string = ";

or use directive using namespace std;

There is no need to define variable i because it is used only inside the loop.
The loop itself could look the following way

for ( int i = 0; str2[i] = str1[i]; i++ );

Taking into account all comments the code could look the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <conio.h>
#include <cstdlib>

int main()
{
   const int N = 15;
   char str1[N], str2[N];

   std::cout << "Enter any string (no more than " << N << " char) = ";

   std::cin.getline( str1, N );

   for ( int i = 0; str2[i] = str1[i]; i++ );


   std::cout << str2;

   getch();

   return 0;
}




Last edited on
It may depend which version of the compiler you use. I have several different compilers. http://orwelldevcpp.blogspot.com/ is the latest devc++. Broadly speaking http://www.codeblocks.org/ is more highly recommended.

Anyway, I had to modify the code for Dev-C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <conio.h>

    using std::cout;
    using std::cin;
int main()
{
    char str1[15], str2[15];

    int i;
    cout<<"Enter any string = ";

    cin>>str1;

    for(1=0,str1[i]!='\0';i++)

    str2[i]='\0';

    cout<<str2;

    getch();
}

Then I get the following two compiler errors;
1
2
[Error] lvalue required as left operand of assignment
[Error] expected ';' before ')' token

and the cursor is positioned on the exact location of the error. The fix should be straightforward if you are familiar with for-loops, but you may need keen eyesight to look closely at the code.
Thanks Vlad and Chervil
Topic archived. No new replies allowed.