153k views
0 votes
Write a function that takes a character string value and prints out the bytes stored in memory of its representation (including the terminating 0). Test it on a string containing your full name.

User Agoldis
by
3.9k points

1 Answer

7 votes

Answer:

C++ code explained below

Step-by-step explanation:

SOURCE CODE:

*Please follow the comments to better understand the code.

#include <stdio.h>

int numOfBytes(char* string)

{

// initialise the variables

int i=0,size=0;

// while the string reaches to \0, repeat the loop.

while(string[i]!='\0')

{

// add that size

size += sizeof(*string);

// increase the i value.

i++;

}

// add 1 byte for \0

size = size+1;

return size;

}

int main() {

char name[]="Praveen Kumar Reddy";

printf("The size is %d bytes.",numOfBytes(name));

return 0;

}

=============

User Thomasmeadows
by
3.6k points