OOP CHAR ARRAY

HAPPY 2021 everyone!

I am stuck with a simple task and asking for any reference , help or recommendation!

I should remark that the code contains some other functions as well, but the single problem which I am stuck with is creating a char array property and write a function which will calculate the number of elements of the char array; ( lines 88-93 , 129-132)

That's what I've done so far:

Many thanks lads!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string.h>

using namespace std;

class var
{ 
      int value; 
      
      char sir[255];
      
   public:
    
     var(int value);
    
     var(char sir[]);
     
     var(int value, char sir[]);
     
     var(const var&Obj);
     
    ~var(){ cout<<" DESTRUCTOR value = "<<endl; }

    
    
 void setVALUEm();
 
 inline const int getVALUE()const{ return this->value; } 
 
 
 void setCHARm();
 
 inline const char* getCHAR()const{ return this->sir; }
 
 
 
    friend void  ABC(var& , var& , var&, var&);  
    
    friend  int ABC1(var& , var& , var& );
    
    friend void POW(var&);
    
    friend void parity(var&);
    
    friend void calc(var&);
    
};

     var::var(int value=NULL){  this->value=value; this->sir[0]=0; } 
     
     var::var(char sir[]) { this->value=NULL; (this->sir,sir); }
     
     var::var(int value, char sir[]){ this->value=value; strcpy(this->sir,sir);}
     
     var::var(const var&Obj){   this->value = Obj.value; strcpy(this->sir,Obj.sir); }
    
     
     
     
void var::setVALUEm(){ cout<<" Enter value="; cin>>this->value; }

void var::setCHARm(){ cout<<" Enter value char="; cin>>this->sir; }

void ABC(var&a, var&b, var&c, var&R ) { R.value=a.value+b.value-c.value;  cout<<" friend a+b-c="<<R.value<<endl; }

int ABC1(var&a, var&b, var&c){ return a.value+b.value-c.value; }


void POW(var&X)
{
	int p;
	cout<<"enter p= "<<endl;
	cin>>p;
	
	X.value= pow(X.value, p);
}


void parity(var&X)
{
	if(X.value%2==0) cout<<"EVEN"<<endl;
 else cout<<"ODD"<<endl;
}


void calc(var&X)
{

strlen(X.sir);
	
}



int main()
{
 var A=10,B=11,C=12,R, r=ABC1(A,B,C), Y; 
 
 cout<<" a+b-c="<<A.getVALUE()+B.getVALUE()-C.getVALUE()<<endl;
 
 ABC(A,B,C,R);
 
 cout<<" R="<<R.getVALUE()<<endl;
 
 cout<<" r="<<r.getVALUE()<<endl;
 cout<<endl;
 
 POW(A);
  POW(B);
   POW(C);
   
   cout<<"A="<<A.getVALUE()<<endl;
   cout<<"B="<<B.getVALUE()<<endl;
   cout<<"C="<<C.getVALUE()<<endl;
   
   cout<<endl;
   
   parity(A);
    parity(B);
     parity(C);
   
   cout<<"A="<<A.getVALUE()<<endl;
   cout<<"B="<<B.getVALUE()<<endl;
   cout<<"C="<<C.getVALUE()<<endl;
   cout<<endl;
   
  Y.setCHARm();
  cout<<" CHAR array= "<<Y.getCHAR()<<endl;
  calc(Y);
  cout<<" CHAR array 1= "<<Y.getCHAR()<<endl;
  
 return 0;
}
Last edited on
I've removed the non-char code et al. So consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
include <iostream>
#include <cstring>

using namespace std;

class var
{
	static const size_t maxchar {255};

	int value {};
	char sir[maxchar] {};

public:
	var(int value = 0);
	var(const char* const sir);
	var(int value, const char* const sir);
	var(const var& Obj);

	void setCHARm();
	inline const char* getCHAR() const { return sir; }
};

var::var(int val) : value(val) {}
var::var(const char* const s) { strcpy(sir, s); }
var::var(int val, const char* const s) : value(val) { strcpy(sir, s); }
var::var(const var& Obj) : value(Obj.value) { strcpy(sir, Obj.sir); }

void var::setCHARm() { cout << " Enter value char="; cin.getline(sir, 255); }

int main()
{
	var Y("qwerty");

	cout << " CHAR array= " << Y.getCHAR() << '\n';

	Y.setCHARm();
	cout << " CHAR array= " << Y.getCHAR() << '\n';
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <Windows.h>
#include<string>
#include<iostream>
using namespace std;

int strlen(char* c) {

	int len{};

	while (c != nullptr && *c!='\0') {
		len++;
		++c;
			
		}
	return len;
}

int strlen2(char* c) {
	return string(c).size();
}


int main() {

	char a[255]="abcdefghijklmnopqrstuvwxyz";

	cout << strlen(a)<<'\n';
	cout << strlen2(a);
}
If you're going to 'roll your own' strlen() function, then:

1
2
3
4
5
6
7
8
size_t strlen1(const char* c) {
	auto c1 {c};

	if (c1)
		for (; *c1; ++c1);

	return c1 - c;
}

FreeBSD libc (FreeBSD-2-Clause license):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Portable strlen() for 32-bit and 64-bit systems.
 *
 * Rationale: it is generally much more efficient to do word length
 * operations and avoid branches on modern computer systems, as
 * compared to byte-length operations with a lot of branches.
 *
 * The expression:
 *
 *	((x - 0x01....01) & ~x & 0x80....80)
 *
 * would evaluate to a non-zero value iff any of the bytes in the
 * original word is zero.
 *
 * On multi-issue processors, we can divide the above expression into:
 *	a)  (x - 0x01....01)
 *	b) (~x & 0x80....80)
 *	c) a & b
 *
 * Where, a) and b) can be partially computed in parallel.
 *
 * The algorithm above is found on "Hacker's Delight" by
 * Henry S. Warren, Jr.
 */

/* Magic numbers for the algorithm */
#if LONG_BIT == 32
static const unsigned long mask01 = 0x01010101;
static const unsigned long mask80 = 0x80808080;
#elif LONG_BIT == 64
static const unsigned long mask01 = 0x0101010101010101;
static const unsigned long mask80 = 0x8080808080808080;
#else
#error Unsupported word size
#endif

#define	LONGPTR_MASK (sizeof(long) - 1)

/*
 * Helper macro to return string length if we caught the zero
 * byte.
 */
#define testbyte(x)				\
	do {					\
		if (p[x] == '\0')		\
		    return (p - str + x);	\
	} while (0)

size_t
strlen(const char *str)
{
	const char *p;
	const unsigned long *lp;
	long va, vb;

	/*
	 * Before trying the hard (unaligned byte-by-byte access) way
	 * to figure out whether there is a nul character, try to see
	 * if there is a nul character is within this accessible word
	 * first.
	 *
	 * p and (p & ~LONGPTR_MASK) must be equally accessible since
	 * they always fall in the same memory page, as long as page
	 * boundaries is integral multiple of word size.
	 */
	lp = (const unsigned long *)((uintptr_t)str & ~LONGPTR_MASK);
	va = (*lp - mask01);
	vb = ((~*lp) & mask80);
	lp++;
	if (va & vb)
		/* Check if we have \0 in the first part */
		for (p = str; p < (const char *)lp; p++)
			if (*p == '\0')
				return (p - str);

	/* Scan the rest of the string using word sized operation */
	for (; ; lp++) {
		va = (*lp - mask01);
		vb = ((~*lp) & mask80);
		if (va & vb) {
			p = (const char *)(lp);
			testbyte(0);
			testbyte(1);
			testbyte(2);
			testbyte(3);
#if (LONG_BIT >= 64)
			testbyte(4);
			testbyte(5);
			testbyte(6);
			testbyte(7);
#endif
		}
	}

	/* NOTREACHED */
	return (0);
}

https://cgit.freebsd.org/src/tree/lib/libc/string/strlen.c?h=stable/12


GNU glibc (GNU Lesser General Public License)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* Return the length of the null-terminated string STR.  Scan for
   the null terminator quickly by testing four bytes at a time.  */
size_t
STRLEN (const char *str)
{
  const char *char_ptr;
  const unsigned long int *longword_ptr;
  unsigned long int longword, himagic, lomagic;

  /* Handle the first few characters by reading one character at a time.
     Do this until CHAR_PTR is aligned on a longword boundary.  */
  for (char_ptr = str; ((unsigned long int) char_ptr
			& (sizeof (longword) - 1)) != 0;
       ++char_ptr)
    if (*char_ptr == '\0')
      return char_ptr - str;

  /* All these elucidatory comments refer to 4-byte longwords,
     but the theory applies equally well to 8-byte longwords.  */

  longword_ptr = (unsigned long int *) char_ptr;

  /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
     the "holes."  Note that there is a hole just to the left of
     each byte, with an extra at the end:

     bits:  01111110 11111110 11111110 11111111
     bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD

     The 1-bits make sure that carries propagate to the next 0-bit.
     The 0-bits provide holes for carries to fall into.  */
  himagic = 0x80808080L;
  lomagic = 0x01010101L;
  if (sizeof (longword) > 4)
    {
      /* 64-bit version of the magic.  */
      /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
      himagic = ((himagic << 16) << 16) | himagic;
      lomagic = ((lomagic << 16) << 16) | lomagic;
    }
  if (sizeof (longword) > 8)
    abort ();

  /* Instead of the traditional loop which tests each character,
     we will test a longword at a time.  The tricky part is testing
     if *any of the four* bytes in the longword in question are zero.  */
  for (;;)
    {
      longword = *longword_ptr++;

      if (((longword - lomagic) & ~longword & himagic) != 0)
	{
	  /* Which of the bytes was the zero?  If none of them were, it was
	     a misfire; continue the search.  */

	  const char *cp = (const char *) (longword_ptr - 1);

	  if (cp[0] == 0)
	    return cp - str;
	  if (cp[1] == 0)
	    return cp - str + 1;
	  if (cp[2] == 0)
	    return cp - str + 2;
	  if (cp[3] == 0)
	    return cp - str + 3;
	  if (sizeof (longword) > 4)
	    {
	      if (cp[4] == 0)
		return cp - str + 4;
	      if (cp[5] == 0)
		return cp - str + 5;
	      if (cp[6] == 0)
		return cp - str + 6;
	      if (cp[7] == 0)
		return cp - str + 7;
	    }
	}
    }
}

https://sourceware.org/git/?p=glibc.git;a=blob;f=string/strlen.c
Topic archived. No new replies allowed.