Deleting undesired characters at the end of a char [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
With this code below I dont know how to delete the undesired characters appearing at the end of the message array. It is compulsory for me to use char, can't use strings, because of the rest of my code.
recvbuf is also a char* recvbuf=new char
char* message=new char[140];
for (int i=1; i<141; i++){
message[i-1]=recvbuf[i];
}
printf("Message: %s\n", message);
delete[]recvbuf;

Though it is recommended you use strings to implement this code, the problem can be fixed by manually appending a null character \0 at the end of your char array.
You can introduce it as:
char* message=new char[141];
for (int i=1; i<141; i++){
message[i-1]=recvbuf[i];
}
message[140] = '\0'; //newly introduced line.
printf("Message: %s\n", message);
delete[]recvbuf;
NOTE 1: The size of the array was increased from 140 to 141 during initialization to make room for the \0 character at the end.
Cheers!

Related

How can I add letters in a sentence? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I've asked to write code that gets a char array(sentence), if the there is an 'i' in the sentence I need to add the letter 'b' the letter 'i' again like this example:
pig -> pibig
I tried to use string.h functions but I didn't succeed to make it right.
Use std::string in string header file, and std::string::insert whenever you need to insert a char in string:
std::string my_string = "my satringa";
for (size_t i = 0; i < my_string.length(); ++i)
{
if (my_string.at(i) == 'a')
{
my_string.insert(i + 1, "b");
}
}
std::clog << my_string << std::endl;
Output:
> my sabtringab
If you are forced to use C-style strings, don't worry do all of your operations on std::string and then take the underlying stored string with std::string::c_str() as a C-style string (and don't forget to take a copy).

how to write correctly into multidimentional char array unknown amount of values but fixed amound of chars [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm sick and tired of solving why my ch[0] is of value "Thomas EdisonÇ#", when it should be "Thomas Edison"
int main(){
using namespace std;
ifstream in("U2.txt");
int n;
in>>n; //n=rows, so in every line there will be "name surname", time, money
char ch[n][21]; //I'm trying to get Name+Surname which must be 20 char long
in.read(ch[0], 20);
cout << ch[0]; //but getting Thomas EdisonÇ#
return 0;}
It works on one dimentional ch[21], but there's gonna be lots of values so I want to use ch[n][21]
Any other out of my box solution is welcome, I'm tired
You are forgetting that C strings need to be nul terminated
in.read(ch[0], 20);
ch[0][20] = '\0'; // add the nul terminator
cout << ch[0]; // now correct output

How to check array size in c++ avoiding empty spaces [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am making a password program, when you input a password from a character array and i want to check if all the array spaces are used. If c++ auto fills empty brackets how can i evaluate?
You should be using std::getline to read a line as a std::string instead of reading into an array.
What happens when the user enters a password that's longer than your 10 characters? UB happens, and you don't want that.
std::string password;
std::getline(std::cin, password);
if (password.size() != 10) {
// Take appropriate action.
}
One method is to initialize the array with zeroes, fill it with password from the user and then look for the first zero in the array, which will tell you how many characters the user entered.
char password[10];
std::fill(password, sizeof(password), 0);
std::cin >> password;
size_t realEnteredPasswordLength = strlen(password);
To check the number of characters read you can do the following:
char c[10];
std::cin >> c;
std::cout << strlen(c);
You do not have to initialize the array to zeroes before using istream operator >>. An additional null character will be automatically appended at the end of the array. Keep in mind that this will only read the whole password if it does not contain any whitespace characters. To read the whole line, you can do this instead
char c[10];
std::cin.getline(c, 10);
std::cout << strlen(c);
This will read at most 10 characters from the next line, including a null terminator, and store them in the array c. This has the added bonus of protecting you against buffer overflows.
If you don't specify a maximum length for the password and using a char array is not a requirement you need to adhere to, I would suggest using a std::string instead
std::string s;
getline(std::cin, s);
std::cout << s.size();

How to organize infinite loop with symbols analysis in it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to organize infinite loop with symbol analysis in it. In C I used fgets(buf, N, stdin), suppose buf is buf[10]. User could type string of any length and I could analyze it by breaking down the input and examining parts of length 10. How can I implement this in C++ without using C libraries. Sorry for my English if you can't understand what I mean
In C++ you should std::cin to read from standard input.
// #include <iostream>
do
{
char buf[10]{}; // create array of 10 bytes filled with zeros.
std::cin.read(buf, 10); // read 10 bytes
// at this point you should check if std::cin.read succeeded.
// otherwise you will be reading zeros.
std::streamsize numRead = std::cin.gcount(); // obtain number of read bytes.
std::cout << numRead << " " << buf << std::endl; // some printing.
}while(std::cin);

ASCII values in table [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to initialise the table with all the ascii characters i.e. at 65-A, 66-B ....
Table abc;
for(int ascii=0;ascii<256;ascii++)
{
string a;
a=ascii;
abc.insertvalue(a,ascii);
//I have a class named table which has insertvalue function
}
The code shows error after inserting the 127th ascii character.
How can I modify it.
While debugging, it only printed till 127th position of array.
ASCII is a 7bit encoding. You should change the loop to while (ascii < 128).
And use a for loop:
for (int ascii = 0; ascii < 128; ++ascii) {
…
}