How to print inputted char array seperately

Hello fellow C++ learners! I'm very new to this Array concept and I'm having trouble with doing a certain task I made up for myself.

The QUESTION IS:

INPUT: series of alphabets less than 100
ex: experiences

OUTPUT: print alphabets 2 by 2
ex: ex
pe
ri
en
ce
s

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main() {
	char s1[101];
	cin >> s1;
	if (s1[101] == 'NUL') {
		for (int i = 0; i < 101; i++) {
			if (s1[i] == 'NUL') {
				break;
			}
			else
				cout << s1[i];
			i++;
			if (s1[i] == 'NUL') {
				break;
			}
			else
				cout << s1[i];
			cout << endl;
		}
	}
	return 0;
}


this is what I did, knowing that NUL comes after any char array. Did I do something wrong here?
this is what I did, knowing that NUL comes after any char array. Did I do something wrong here?

First this 'NUL' is a multi-character constant, not a null character.

Second a "null" character is not automatically placed at the end of an array of char.

Third in C/C++ the "null" character is '\0' not 'NUL'.

Fourth be careful about incrementing your loop control variable (i) outside the loop body as this will increment the variable multiple times and in this loop which could cause you to access the array out of bounds.

Fifth Why aren't you using std::strings instead of the more dangerous character arrays?

Last edited on
s1[101] is out of bounds, don't try to access it. Not only on line 4, you may also access it in line 12

¿what's the meaning of line 4?

line 4... is 'nul' a new way to write 0?

something like this though..
I am assuming the string is valid null terminated etc. If you need to check this, add it in.

int lc = 0;
while (s1[lc])
{
cout << s1[lc++];
if (lc %2 == 0) //its +1, so 0th is 1, 1st is 2, write 0th and 1th then endl, etc...
cout << endl;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
   const int MAXSIZE = 101;
   char text[MAXSIZE];

   const int COLS = 2;

   cout << "Input some text (maximum " << MAXSIZE - 1 << " characters): ";
   cin.getline( text, MAXSIZE );                           // shouldn't overrun character array; extracts '\n' from the stream

   int i= 0;                                               // start of text
   while ( text[i] != '\0' )                               // loops until null terminator
// while ( text[i] )                                       // alternative - works just as well
   {
       cout << text[i];                                    // output character
       if ( ( i + 1 ) % COLS == 0 ) cout << '\n';          // start a newline when it reaches a multiple of COLS
       i++;                                                // increment counter
   }
   cout << '\n';
}


Input some text (maximum 100 characters): This is Manchester; we do things differently here.
Th
is
 i
s 
Ma
nc
he
st
er
; 
we
 d
o 
th
in
gs
 d
if
fe
re
nt
ly
 h
er
e.



Alternatively, use a normal for loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
   const int MAXSIZE = 101;
   char text[MAXSIZE];

   const int COLS = 2;

   cout << "Input some text (maximum " << MAXSIZE - 1 << " characters): ";
   cin.getline( text, MAXSIZE );

   for ( int i = 0; i < strlen( text ); i++ )       
   {
       cout << text[i];
       if ( ( i + 1) % COLS == 0 ) cout << '\n';           // start a newline when it reaches a multiple of COLS
   }
   cout << '\n';
}


or pointers:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main()
{
   const int MAXSIZE = 101;
   char text[MAXSIZE];

   const int COLS = 2;

   cout << "Input some text (maximum " << MAXSIZE - 1 << " characters): ";
   cin.getline( text, MAXSIZE );                           // shouldn't overrun character array; extracts '\n' from the stream

   int counter = 0;
   char *p = text;                                         // point to start of text
   while ( *p )                                            // loops until it hits the null terminator
   {
       cout << *p;                                         // output character
       if ( ++counter % COLS == 0 ) cout << '\n';          // start a newline when it reaches a multiple of COLS
       p++;                                                // increment pointer to point to next position
   }
   cout << '\n';
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
 
int main() {
    char s1[102];
    cin >> s1;
        for (int i = 0; i < 101; i++) {
            if (s1[i] == '\0') {
                break;
            }
            else
                cout << s1[i];
            i++;
            if (s1[i] == '\0') {
                break;
            }
            else
                cout << s1[i];
            cout << endl;
        }
    return 0;
}


thank you
jlb
for your third tip!
Third in C/C++ the "null" character is '\0' not 'NUL'.


Also,
ne555
for line 4

if (s1[101] == 'NUL') {

I was trying to set parameter of input thinking that if the word was longer than 100alphabets
the 101th s1 would have something other than NULL or '\0' in it.
So I just thought using an "if statement" to check whether the 101th s1 has NULL or not could help the code to not work when more than 100alphabets were entered.


jonnin
I think I will try your way out to shorten my code! thanks!

lastchance
A whole lot of things I don't quite get yet, but still, I will try to do my research!
Thankyou for all the // you added to help me understand!

Like I said, I am completely new to this whole coding concept, and I have no idea about most of the statement priority or settings(?). I am also not learning code in an orthodox way haha ;) I guess I'm trying out concepts one by one in a weird order haha.



Great Thanks to everyone who commented!
if you are learning, get away from character array strings as fast as you can, use string type instead. Its ok for your first few programs, but the sooner you get away from them, the better off you will be.


Topic archived. No new replies allowed.