input data to the 2-d array how this work? - c++

char board[5][6];
for (int i = 0; i<5; i++)
cin >> board[i];
URLPM
XPRET
GIAET
XTNZY
XOQRS
Why do they use only one variable board[i] not instead board boardp[i][i]?
I do not understand how this works.
And if I do:
cout << board[4]
It shows:
XTNZY
XOQRS
All the output below line 4...
I did not understand the procedure for this grammar.

In C/C++, a string can be represented in an array of char, and it should be null-terminated characters.
To store "abcd", for example, you need a char array with size of 5(including null character).
char str[5]; // store 4 characters => a, b, c, d, \0
When you declare the following 2-dimensional array, you make an array of 5 'char[6]'. And the reserved memory is not initialized(since it's local variable), but in your case there are 0's, which is considered as null.
char board[5][6];
So if you put 5-length characters in board[i], you will have null-terminated characters though it's not a good practice. But if you put 6-length characters in board[i], your string will be connected to the string stored in board[i+1] because there is no end of the string(null). If try to print board[i] with cout or printf(), it will display board[i] and board[i+1] since they print sequence of char's until facing null.

Related

char string doesn't work what the problem with "\0"

I don't know what the problem is? My code doesn't work. :(
#include <iostream>
#include <string.h>
using namespace std;
bool isPalindrome(char* word){
int len = strlen(word);
if(len <= 1){
return true;
}else
if(word[0] == word[len-1]){
char n[len-1];
n[len-1]= "\0";
return isPalindrome(n);
}
return false;
}
int main(){
char *a = "alla";
bool b = isPalindrome(a);
cout<<b<<endl;
return 0;}
The error is "\0" , I don't know why.
My main function should be not right too.
I believe "\0" is a null-terminated 2 character C string consisting of a zero followed by a zero. Use '\0' instead, which is just a zero. Remember: use double quotes for strings and single quotes for single characters. You cannot assign a string to a string index location, so double quotes don't work like that, but you can assign a character to a string index location.
2nd error: read the documentation on strlen(). Create n with char n[len + 1];, NOT char n[len-1];, in order to make n the same length as the other string. Then, null terminate with n[len]= '\0';, not n[len - 1]= '\0';, since strlen doesn't count the null terminator in the string. I'm confused by your code though: what is the purpose of this n string? Lastly, you're writing outside your n array as you have it written! Since you made n have size len - 1, you would need to null terminate at index len - 2. len - 1 in your case is outside the array! Always null terminate inside the array at the index 1 smaller than the size of the array. When the compiler knows the side of the array, such as is the case with n, do it like this instead: n[sizeof(n) - 1] = '\0';.
You can't use a variable to set an array length in C by the way, and in C++ it may require len to be const to instantiate an array.
The Simple answer is that you should
use '\0' instead of "\0"
as n is a character array that you should use single quotation instead of double quotation
Hope this will Help
The assignment of element of character array should be with character only but in your case you are assigning to string. Note that ''/0'' is string. Try using assigning to '/0'.

setting char arrays equal to eachother with isdigit and isalpha

Im trying to set a char array equal to 2 other arrays depending on if the element in the first array is a number or a letter. The code makes logical sense to me but the output for the 2 other strings after the for loop doesn't correspond to the logic. Is it because of a missing null value somewhere in the other 2 loops or is the code itself invalid? arrayAlpha, arrayNum, and palind are all char arrays set to a length of 30 elements while string length was already determined before the for loop began.
for(int k=0; k<=stringLength; k++)
{
if( isalpha(palind[k])){
arrayAlpha[k]=palind[k];}
if ( isdigit(palind[k]))
{
arrayNum[k]=palind[k];
}
}
Given the input:
char palind[30] = "12345abcde";
arrayAlpha is garbage.
arrayNum is "12345"
However,
char palind[30] = "abcde12345";
arrayAlpha is "abcde".
arrayNum is garbage.
Thus, [k] is the problem when used in your arrayNum or arrayAlpha which doesn't start with 0.
Simple change will just be subtracting the length of the other.
arrayAlpha[k - strlen(arrayNum)] = palind[k];
arrayNum[k - strlen(arrayAlpha)] = palind[k];
since lengthOfPalind = lengthOfArrayAlpha + lengthOfArrayNum assuming palind only contains letters or numbers.

Shift cipher in C++ (How to get ASCII value and handling numbers)

I have a program set up already to read in a file and split each line into words, storing them into a double vector of strings. That is,
std::vector < std::vector <std::string> > words
So, the idea is to use an array from alphabet a-z and using the ASCII values of the letters to get the index and swapping the characters in the strings with the appropriate shifted character. How would I get the value of each character so that I can look it up as an index?
I also want to keep numbers intact, as a shift cipher, I believe, doesn't do anything with numbers in the text to be deciphered. How would I check if the character is an int so I can leave it alone?
If you want the ASCII value, you simply have to cast the value to a int:
int ascii_value = (int)words[i][j][k];
If you want to have a value starting from A or a you can do this:
int letter_value_from_A = (int)(words[i][j][k] - 'A');
int letter_value_from_a = (int)(words[i][j][k] - 'a');
Your char is nothing else than a value. Take this code as example (I am used to program C++11, so this will be a little ugly):
char shiftarray[256] = {0, 0, 0, 0 // Here comes your map //
std::string output;
for(int w=0; w<words.length(); w++)
{
for(int c=0; c<words[w].length(); c++)
{
output.pushback(shiftarry[words[w][c]]);
}
output.push_back(' ');
}
I do not know how to do it in anything other than basic, but very simply get the ascii value of each letter in the string using a loop. As the loop continues add a value to, or subtract a value from the ascii value you just obtained, then convert it back to a letter and append it to a string. This will give you a different character than you had originally. By doing this, you can load and save data that will look like gibberish if anyone tried to view it other than in the program it was written in. The data then becomes a special propriatry document format.

converting integer into char explictly

I am trying to convert integer into character. I know how to convert character to integer like this int(a) where a is a character. But when I am trying to convert integer to character, it is giving me a symbolic value. Please help me out.
I am doing something like below. Thanks in advance.
int a=0;
char str1[20];
for(int i=0;i<size;i++)
//somecalculation that sets value in a everytime and stores in str1
str1[i]=char(a)-'A'
Well I am running for loop and setting values in str1. This is just little of my code.
You could use str1[i] = static_cast<char>(a + '0');. This will convert a = 0 to '0', a = 1 to '1' etc. Consider the behaviour as undefined outside the range 0, ..., 9.
just use sprintf:
for(int i=0;i<size;i++)
//somecalculation that sets value in a everytime and stores in str1
sprintf(str1 + i, "%i", a);
since you noted that a is each time only a one digit integer, this should work, but this is not very error prone... normally you should check on how much digits were written:
for(int i=0;i<size;i++)
//somecalculation that sets value in a everytime and stores in str1
if (sprintf(str1 + i, "%i", a) != 1)
printf("expected to print only one character!\n");

Pointers & Arrays Pointing Array issue

Firstly, Sorry about my bad english.
I wanna ask something that I expect amazing. I'm not sure this is amazing for everyone, but It is for me :)
Let me give example code
char Text[9] = "Sandrine";
for(char *Ptr = Text; *Ptr != '\0'; ++Ptr)
cout << Ptr << endl;
This code prints
Sandrine
andrine
ndrine
drine
rine
ine
ne
e
I know it's a complicated issue in C++. Why İf I call Ptr to print out screen it prints all of array. However if Text array is a dynamic array, Ptr prints only first case of dynamic array(Text). Why do it happen? Please explain C++ array that how it goes for combination of pointing array.
thanks for helping.
There is nothing particular special about arrays here. Instead, the special behavior is for char const*: in C, pointers to a sequence of characters with a terminating null characters are used to represent strings. C++ inherited this notion of strings in the form of string literals. To support output of these strings, the output operator for char const* interprets a pointer to a char to be actually a pointer to the start of a string and prints the sequence up to the first null character.
When you write
char Text[9] = "Sandrine";
the "Text" is an address in memory, it is the starting address of your string and in its first location there is a 'S' followed by the rest of the characters. A string in C is delimited by a \0 i.e. "S a n d r i n e \0"
When you write
for(char *Ptr = Text; *Ptr != '\0'; ++Ptr)
cout << Ptr << endl;
when the for loop runs the first time it prints the whole string because Ptr points to the start of the string char* Ptr = Text when you increment Ptr
you are pointing to the next character Text + 1 i.e. 'a' and so on once Ptr finds \0 the for loop quits.