After creating a char array, it is filled with trash - c

I enter a word, and i need to create new word with same amount of symbols, and new word must be filled only with '_'.
int main()
{
char word[30];
cout << "Enter word: ";
gets_s(word);
cout << word;
int k = strlen(word);
cout << "Amount of letters in word: "<< k << endl;
char *temp = new char[k];
for (int i = 0; i < k; i++)
{
temp[i] = '_';
}
cout << temp << endl;
}

With C strings, which are really obnoxious to use and can usually be avoided in C++, you must NUL terminate your character buffers:
char *temp = new char[k + 1];
for (int i = 0; i < k; i++)
{
temp[i] = '_';
}
temp[k] = 0; // Terminated
Without that you'll read on into random memory and see all sorts of garbage.
This would be a lot easier with std::string:
std::string temp;
for (int i = 0; i < k; ++i) {
temp += '_';
}
You don't have to remember to NUL terminate because std::string doesn't need it, the Standard Library uses a different method internally and it's handled for you automatically.
But wait, there's more!
If you act now and use std::string you have access to amazing tools like this:
std::string temp(k, '_');
Even easier!

in c++ , when you create array or struct in runtime (dont matter in heap or in stack) all values filled with trash.
to prevent possible error :
for structs :
typedef struct
{
int age;
char name[30];
}person;
person emptyPerson; // emptyPerson age is 0 , name is {0,0,0...}
void runtimeCreatePerson()
{
person runtTimePerson;
cout << runtTimePerson.name<< endl; // this will print garbage values
person runtTimePerson2 = emptyPerson; // now runtimePerson has same values with emptyResponse(copy of)
cout << runtTimePerson2.name<< endl; // this will print ""
}
int main()
{
runtimeCreatePerson();
}
for Array you have to fill it after create:
int i = 0;
int main()
{
char runtimeArray[50];
cout << runtimeArray<< endl; // this will print garbage values
for(i=0 ; i < 50 ; i++)
runtimeArray[i] = 0;
cout << runtimeArray<< endl; // this will print ""
}

Related

Trouble reading string text into an array and counting letters

I am tasked to write a letter histogram which should be able to read any text and count the letters and output them in a neat way.
My question for this code so far is how to read the text into an array, I am stuck. I use two functions for this, one to read the text string into an array, and another function to output the array in this manner:
A: x
B: x
C: x
n: x
#include <iostream>
#include <string>
#include <cctype>
#include <cmath>
using namespace std;
const int ANTAL_BOKSTAVER = 26;
void berakna_histogram_abs ();
void skriv_histogram_abs (int bokstav, int antal);
int main()
{
int i;
char alfabet;
string text;
cout << "Skriv in texten: ";
getline (cin, text);
void berakna_histogram_abs();
void skriv_histogram_abs();
return 0;
}
void berakna_histogram_abs ()
{
string text;
char str[100]= {getline};
int i= 0, alfabet[26]={0}, j;
while (str[i]!='\0')
{
if (str[i] >= 'a' && str[i]<='z')
{
j = str[i] - 'a';
++alfabet[j];
}
i++;
}
}
void skriv_histogram_abs()
{
int alfabet[26]={0};
int i;
cout << "Frekvensen av alla bokstäver är: " <<endl;
for (i=0; i < 26; i++)
cout << char(i + 'a')<<" : " << alfabet[i] << endl;
}
One can create and pass an array as below.
Create the array once.
Pass it as parameter. The variable and parameter names may differ.
So I renamed the text variable as demonstration.
For a histogram one needs to scale the length.
int main()
{
int i;
string in_text;
cout << "Skriv in texten: ";
getline (cin, in_text);
int alfabet[26] = {0};
berakna_histogram_abs(in_text, alfabet);
skriv_histogram_abs(alfabet);
return 0;
}
void berakna_histogram_abs (string text, int* alfabet)
{
}
void skriv_histogram_abs(int* alfabet)
{
int maxfreq = 0;
int i;
for (i=0; i < 26; i++)
if (alfabet[i] > maxfreq)
maxfreq = alfabet[i];
double ratio = 60.0 / (maxfreq == 0 ? 1 : maxfreq);
cout << "Frekvensen av alla bokstäver är: " <<endl;
for (i=0; i < 26; i++)
{
cout << char(i + 'a')<<" : ";
int laengd = (int)(alfabet[i] * ratio);
for (int j = 0; j < laengd; ++j)
cout << '■';
cout << " " << alfabet[i] << endl;
}
}
Arrays are generally passed as pointers.
I scaled the bar to some 60 characters, so the line length is kept below 80.

Letter histogram doesn't output capital letters. How do I make the outpout code a function?

In this code I am trying to make a letter histogram from a string, and it works for non-capital letters and does not output any capital letters. How does that happen? How would I put the output in a fucntion? Haven't gotten that to properly work either.
#include <iostream>
#include <string>
#include <cctype>
#include <cmath>
using namespace std;
const int ANTAL_BOKSTAVER = 26;
void berakna_histogram_abs (const string &strang);
void skriv_histogram_abs ();
int main()
{
string in_text;
cout << "Skriv in texten: ";
getline(cin, in_text);
berakna_histogram_abs(in_text);
//skriv_histogram_abs();
return 0;
}
void berakna_histogram_abs(const string &strang)
{
string in_text[ANTAL_BOKSTAVER];
int frekvens[ANTAL_BOKSTAVER];
for (int i=0; i<ANTAL_BOKSTAVER; i++)
frekvens[i]=0;
for (int i = 0; i< (int) strang.length(); i++)
{
int index;
if (strang.at(i)>='a' && strang.at(i)<='z')
{
index = strang.at(i) - 'a';
frekvens[index]++;
}
if (strang.at(i)>='A' && strang.at(i)>='Z')
{
index = strang.at(i) - 'A';
frekvens[index]++;
}
}
cout << "\nBokstav\tFrekvens\n";
for (int i = 0; i< ANTAL_BOKSTAVER; i++)
{
char b = char (i+'A');
if (frekvens[i]) cout << b << "\t" << frekvens[i]<< endl;
}
}

String Reversal using Recurrsion using char*

I want string reversed in-place using Recursion. Though this might be duplicate question but I don't understand error in my code below and I searched for answers before asking here.
#include "Reversal.h"
#include <iostream>
#include <cstdlib>
using namespace std;
int size = 10;
char* Reversal :: rever (char *str, int i, int j)
{
if (i>=j)
return str;
char a;
a = str[i];
str[i] = str[j];
str[j] = a;
rever(str, i+1, j-1);
}
int main()
{
Reversal obj;
char *ptr = new char[size];
char *reverse = new char[size];
cout << " Enter a string \n";
for (int i = 0; i < size-1; i++)
{
cin >> ptr[i];
}
reverse = obj.rever(ptr, 0, strlen(ptr)-1);
for (int k = 0; k < strlen(reverse)-1; k++)
{
cout << reverse[k];
}
return 0;
}
and Reversal.h file is
#ifndef REVERSAL_H_
#define REVERSAL_H_
class Reversal {
public:
Reversal();
char* rever(char*, int,int);
virtual ~Reversal();
};
#endif /* REVERSAL_H_ */
This code has quite a few problems. The first thing you need to decide is whether you are reversing your input string in place, or returning a pointer to a newly allocated string. Since it looks like you planned on filling a second array, let's stick with that:
void Reversal::rever(char* buf, char* str, int i, int j)
{
if (i>=j)
{
buf[strlen(str)] = '\0';
return;
}
buf[j] = str[i];
buf[i] = str[j];
rever(buf, str, i+1, j-1);
}
Called as:
obj.rever(reverse, str, 0, strlen(str)-1);
Note that I would not make this part of a class unless there is some other reason to do so (like if it were instead to return the reverse of a char* contained in the class), and since we are using C++ I would recommend using std::string instead of char*.

Change a character in a string to a string

I'm trying to write code in C that will take a string, check each character for a specific character (call it 'x'), and if the character is 'x', change it to multiple characters (like "yz"). Here is my attempt, assuming buffer and replace are defined arrays of characters (i.e. char buffer[400] = jbxyfgextd...; char replace[250];)
int j = 0;
for (j = 0; j < 110; j++) {
if (buffer[j]=='x') {
int len = strlen(replace);
replace[len] = 'y';
replace[len+1] = 'z';
}
else {
replace[j]=buffer[j];
}
}
When I run this I get some y's and z's, but they are not back to back. Is there any procedure/function to do this easily?
Because index in buffer[] and replace[] arrays are not same. Use two indexes separately.
In your code expression: replace[j] = buffer[j]; is wrong. You can correct it like:
else {
int len = strlen(replace);
replace[len]=buffer[j];
}
But to use strlen(), array replace[] should be nul \0 terminated. (declare replace as char replace[250] = {0})
Edit:
To write better code use two septate indexes as I suggested above -- code will be efficient and simplified.
int bi = 0; // buffer index
int ri = 0; // replace index
for (bi = 0; bi < 110; bi++) {
if (buffer[bi] == 'x') {
replace[ri++] = 'y';
replace[ri] = 'z';
}
else {
replace[ri] = buffer[bi];
}
replace[++ri] = '\0'; // terminated with nul to make it string
}
#include <iostream>
#include <string>
#include <cstring>
int main ( int argc, char *argv[])
{
// Pass through the array once counting the number of chars the final string
// will need then allocate the new string
char buffer[] = "this x is a text test";
char replacement[] = "yz";
unsigned int replaceSize = strlen(replacement);
unsigned int bufferSize = 0;
unsigned int newSize = 0;
// calculate the current size and new size strings
// based on the replacement size
char *x = buffer;
while (*x)
{
if ( *x == 'x' )
{
newSize+=replaceSize;
}
else
{
++newSize;
}
++x;
++bufferSize;
}
// allocate the new string with the new size
// and assign the items to it
char *newString = new char[newSize];
unsigned int newIndex = 0;
for ( unsigned int i = 0; i < bufferSize; ++i )
{
if ( buffer[i] == 'x' )
{
for ( unsigned int j = 0; j < replaceSize ; ++j )
{
newString[newIndex++] = replacement[j];
}
}
else
{
newString[newIndex++] = buffer[i];
}
}
std::string originalS ( buffer );
std::string newS ( newString );
std::cout << "Original: " << originalS << std::endl;
std::cout << "New: " << newS << std::endl;
delete newString;
}

Print an int in binary representation using C

I'm looking for a function to allow me to print the binary representation of an int. What I have so far is;
char *int2bin(int a)
{
char *str,*tmp;
int cnt = 31;
str = (char *) malloc(33); /*32 + 1 , because its a 32 bit bin number*/
tmp = str;
while ( cnt > -1 ){
str[cnt]= '0';
cnt --;
}
cnt = 31;
while (a > 0){
if (a%2==1){
str[cnt] = '1';
}
cnt--;
a = a/2 ;
}
return tmp;
}
But when I call
printf("a %s",int2bin(aMask)) // aMask = 0xFF000000
I get output like;
0000000000000000000000000000000000xtpYy (And a bunch of unknown characters.
Is it a flaw in the function or am I printing the address of the character array or something? Sorry, I just can't see where I'm going wrong.
NB The code is from here
EDIT: It's not homework FYI, I'm trying to debug someone else's image manipulation routines in an unfamiliar language. If however it's been tagged as homework because it's an elementary concept then fair play.
Here's another option that is more optimized where you pass in your allocated buffer. Make sure it's the correct size.
// buffer must have length >= sizeof(int) + 1
// Write to the buffer backwards so that the binary representation
// is in the correct order i.e. the LSB is on the far right
// instead of the far left of the printed string
char *int2bin(int a, char *buffer, int buf_size) {
buffer += (buf_size - 1);
for (int i = 31; i >= 0; i--) {
*buffer-- = (a & 1) + '0';
a >>= 1;
}
return buffer;
}
#define BUF_SIZE 33
int main() {
char buffer[BUF_SIZE];
buffer[BUF_SIZE - 1] = '\0';
int2bin(0xFF000000, buffer, BUF_SIZE - 1);
printf("a = %s", buffer);
}
A few suggestions:
null-terminate your string
don't use magic numbers
check the return value of malloc()
don't cast the return value of malloc()
use binary operations instead of arithmetic ones as you're interested in the binary representation
there's no need for looping twice
Here's the code:
#include <stdlib.h>
#include <limits.h>
char * int2bin(int i)
{
size_t bits = sizeof(int) * CHAR_BIT;
char * str = malloc(bits + 1);
if(!str) return NULL;
str[bits] = 0;
// type punning because signed shift is implementation-defined
unsigned u = *(unsigned *)&i;
for(; bits--; u >>= 1)
str[bits] = u & 1 ? '1' : '0';
return str;
}
Your string isn't null-terminated. Make sure you add a '\0' character at the end of the string; or, you could allocate it with calloc instead of malloc, which will zero the memory that is returned to you.
By the way, there are other problems with this code:
As used, it allocates memory when you call it, leaving the caller responsible for free()ing the allocated string. You'll leak memory if you just call it in a printf call.
It makes two passes over the number, which is unnecessary. You can do everything in one loop.
Here's an alternative implementation you could use.
#include <stdlib.h>
#include <limits.h>
char *int2bin(unsigned n, char *buf)
{
#define BITS (sizeof(n) * CHAR_BIT)
static char static_buf[BITS + 1];
int i;
if (buf == NULL)
buf = static_buf;
for (i = BITS - 1; i >= 0; --i) {
buf[i] = (n & 1) ? '1' : '0';
n >>= 1;
}
buf[BITS] = '\0';
return buf;
#undef BITS
}
Usage:
printf("%s\n", int2bin(0xFF00000000, NULL));
The second parameter is a pointer to a buffer you want to store the result string in. If you don't have a buffer you can pass NULL and int2bin will write to a static buffer and return that to you. The advantage of this over the original implementation is that the caller doesn't have to worry about free()ing the string that gets returned.
A downside is that there's only one static buffer so subsequent calls will overwrite the results from previous calls. You couldn't save the results from multiple calls for later use. Also, it is not threadsafe, meaning if you call the function this way from different threads they could clobber each other's strings. If that's a possibility you'll need to pass in your own buffer instead of passing NULL, like so:
char str[33];
int2bin(0xDEADBEEF, str);
puts(str);
Here is a simple algorithm.
void decimalToBinary (int num) {
//Initialize mask
unsigned int mask = 0x80000000;
size_t bits = sizeof(num) * CHAR_BIT;
for (int count = 0 ;count < bits; count++) {
//print
(mask & num ) ? cout <<"1" : cout <<"0";
//shift one to the right
mask = mask >> 1;
}
}
this is what i made to display an interger as a binairy code it is separated per 4 bits:
int getal = 32; /** To determain the value of a bit 2^i , intergers are 32bits long**/
int binairy[getal]; /** A interger array to put the bits in **/
int i; /** Used in the for loop **/
for(i = 0; i < 32; i++)
{
binairy[i] = (integer >> (getal - i) - 1) & 1;
}
int a , counter = 0;
for(a = 0;a<32;a++)
{
if (counter == 4)
{
counter = 0;
printf(" ");
}
printf("%i", binairy[a]);
teller++;
}
it could be a bit big but i always write it in a way (i hope) that everyone can understand what is going on. hope this helped.
#include<stdio.h>
//#include<conio.h> // use this if you are running your code in visual c++, linux don't
// have this library. i have used it for getch() to hold the screen for input char.
void showbits(int);
int main()
{
int no;
printf("\nEnter number to convert in binary\n");
scanf("%d",&no);
showbits(no);
// getch(); // used to hold screen...
// keep code as it is if using gcc. if using windows uncomment #include & getch()
return 0;
}
void showbits(int n)
{
int i,k,andmask;
for(i=15;i>=0;i--)
{
andmask = 1 << i;
k = n & andmask;
k == 0 ? printf("0") : printf("1");
}
}
Just a enhance of the answer from #Adam Markowitz
To let the function support uint8 uint16 uint32 and uint64:
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
// Convert integer number to binary representation.
// The buffer must have bits bytes length.
void int2bin(uint64_t number, uint8_t *buffer, int bits) {
memset(buffer, '0', bits);
buffer += bits - 1;
for (int i = bits - 1; i >= 0; i--) {
*buffer-- = (number & 1) + '0';
number >>= 1;
}
}
int main(int argc, char *argv[]) {
char buffer[65];
buffer[8] = '\0';
int2bin(1234567890123, buffer, 8);
printf("1234567890123 in 8 bits: %s\n", buffer);
buffer[16] = '\0';
int2bin(1234567890123, buffer, 16);
printf("1234567890123 in 16 bits: %s\n", buffer);
buffer[32] = '\0';
int2bin(1234567890123, buffer, 32);
printf("1234567890123 in 32 bits: %s\n", buffer);
buffer[64] = '\0';
int2bin(1234567890123, buffer, 64);
printf("1234567890123 in 64 bits: %s\n", buffer);
return 0;
}
The output:
1234567890123 in 8 bits: 11001011
1234567890123 in 16 bits: 0000010011001011
1234567890123 in 32 bits: 01110001111110110000010011001011
1234567890123 in 64 bits: 0000000000000000000000010001111101110001111110110000010011001011
Two things:
Where do you put the NUL character? I can't see a place where '\0' is set.
Int is signed, and 0xFF000000 would be interpreted as a negative value. So while (a > 0) will be false immediately.
Aside: The malloc function inside is ugly. What about providing a buffer to int2bin?
A couple of things:
int f = 32;
int i = 1;
do{
str[--f] = i^a?'1':'0';
}while(i<<1);
It's highly platform dependent, but
maybe this idea above gets you started.
Why not use memset(str, 0, 33) to set
the whole char array to 0?
Don't forget to free()!!! the char*
array after your function call!
Two simple versions coded here (reproduced with mild reformatting).
#include <stdio.h>
/* Print n as a binary number */
void printbitssimple(int n)
{
unsigned int i;
i = 1<<(sizeof(n) * 8 - 1);
while (i > 0)
{
if (n & i)
printf("1");
else
printf("0");
i >>= 1;
}
}
/* Print n as a binary number */
void printbits(int n)
{
unsigned int i, step;
if (0 == n) /* For simplicity's sake, I treat 0 as a special case*/
{
printf("0000");
return;
}
i = 1<<(sizeof(n) * 8 - 1);
step = -1; /* Only print the relevant digits */
step >>= 4; /* In groups of 4 */
while (step >= n)
{
i >>= 4;
step >>= 4;
}
/* At this point, i is the smallest power of two larger or equal to n */
while (i > 0)
{
if (n & i)
printf("1");
else
printf("0");
i >>= 1;
}
}
int main(int argc, char *argv[])
{
int i;
for (i = 0; i < 32; ++i)
{
printf("%d = ", i);
//printbitssimple(i);
printbits(i);
printf("\n");
}
return 0;
}
//This is what i did when our teacher asked us to do this
int main (int argc, char *argv[]) {
int number, i, size, mask; // our input,the counter,sizeofint,out mask
size = sizeof(int);
mask = 1<<(size*8-1);
printf("Enter integer: ");
scanf("%d", &number);
printf("Integer is :\t%d 0x%X\n", number, number);
printf("Bin format :\t");
for(i=0 ; i<size*8 ;++i ) {
if ((i % 4 == 0) && (i != 0)) {
printf(" ");
}
printf("%u",number&mask ? 1 : 0);
number = number<<1;
}
printf("\n");
return (0);
}
the simplest way for me doing this (for a 8bit representation):
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
char *intToBinary(int z, int bit_length){
int div;
int counter = 0;
int counter_length = (int)pow(2, bit_length);
char *bin_str = calloc(bit_length, sizeof(char));
for (int i=counter_length; i > 1; i=i/2, counter++) {
div = z % i;
div = div / (i / 2);
sprintf(&bin_str[counter], "%i", div);
}
return bin_str;
}
int main(int argc, const char * argv[]) {
for (int i = 0; i < 256; i++) {
printf("%s\n", intToBinary(i, 8)); //8bit but you could do 16 bit as well
}
return 0;
}
Here is another solution that does not require a char *.
#include <stdio.h>
#include <stdlib.h>
void print_int(int i)
{
int j = -1;
while (++j < 32)
putchar(i & (1 << j) ? '1' : '0');
putchar('\n');
}
int main(void)
{
int i = -1;
while (i < 6)
print_int(i++);
return (0);
}
Or here for more readability:
#define GRN "\x1B[32;1m"
#define NRM "\x1B[0m"
void print_int(int i)
{
int j = -1;
while (++j < 32)
{
if (i & (1 << j))
printf(GRN "1");
else
printf(NRM "0");
}
putchar('\n');
}
And here is the output:
11111111111111111111111111111111
00000000000000000000000000000000
10000000000000000000000000000000
01000000000000000000000000000000
11000000000000000000000000000000
00100000000000000000000000000000
10100000000000000000000000000000
#include <stdio.h>
#define BITS_SIZE 8
void
int2Bin ( int a )
{
int i = BITS_SIZE - 1;
/*
* Tests each bit and prints; starts with
* the MSB
*/
for ( i; i >= 0; i-- )
{
( a & 1 << i ) ? printf ( "1" ) : printf ( "0" );
}
return;
}
int
main ()
{
int d = 5;
printf ( "Decinal: %d\n", d );
printf ( "Binary: " );
int2Bin ( d );
printf ( "\n" );
return 0;
}
Not so elegant, but accomplishes your goal and it is very easy to understand:
#include<stdio.h>
int binario(int x, int bits)
{
int matriz[bits];
int resto=0,i=0;
float rest =0.0 ;
for(int i=0;i<8;i++)
{
resto = x/2;
rest = x%2;
x = resto;
if (rest>0)
{
matriz[i]=1;
}
else matriz[i]=0;
}
for(int j=bits-1;j>=0;j--)
{
printf("%d",matriz[j]);
}
printf("\n");
}
int main()
{
int num,bits;
bits = 8;
for (int i = 0; i < 256; i++)
{
num = binario(i,bits);
}
return 0;
}
#include <stdio.h>
int main(void) {
int a,i,k=1;
int arr[32]; \\ taken an array of size 32
for(i=0;i <32;i++)
{
arr[i] = 0; \\initialised array elements to zero
}
printf("enter a number\n");
scanf("%d",&a); \\get input from the user
for(i = 0;i < 32 ;i++)
{
if(a&k) \\bit wise and operation
{
arr[i]=1;
}
else
{
arr[i]=0;
}
k = k<<1; \\left shift by one place evry time
}
for(i = 31 ;i >= 0;i--)
{
printf("%d",arr[i]); \\print the array in reverse
}
return 0;
}
void print_binary(int n) {
if (n == 0 || n ==1)
cout << n;
else {
print_binary(n >> 1);
cout << (n & 0x1);
}
}

Resources