How to store a string into an an array? [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 8 years ago.
Improve this question
I want to store this string into an an array.
space= 0
A,a =1
B, b =2
C , c = 3
.
.
Z, z= 26
string myArray[26] =
{ "A", "B", "C", "D",”E”,”F”,”G”,”H”,”I”,”J”,”K”,”L”,”M”,”N”,”O”,”P”,
”Q”,”R”,”S”,T”,”U”,”V”,W”,X”,”Y”, ”z” };
for (int i = 0; i < myArray; i++)
{
myArray[] = myArray[i]
cerr << myArray[i] << endl << endl;
}
Is that how to get each character with number?

What you've got is an array of strings, not an array of characters. A string is a container of characters, in the sense that it is capable of holding multiple characters. Your task can be solved with one or two strings, depending on your design preferences (see below).
A, a =1
B, b =2
You are placing two characters per position. However, strings cannot hold more than one character at a single index. If you need both the upper and lower case character to occupy the same spot, you need to make either two strings, or two spots.
Here is the first approach (two strings):
string upper = " ABCDEF...";
string lower = " abcdef...";
int pos = ...; // The desired position
cout << upper[pos] << endl;
cout << lower[pos] << endl;
Here is the second approach (two positions):
string pairs = " AaBbCcDdEeFf...";
int pos = ...; // The desired position
cout << pairs[2*pos] << endl; // Upper
cout << pairs[2*pos+1] << endl; // Lower

Related

How to print two adjacent lines in a loop while keeping both of them in single lines in c++ [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 have two strings in a for loop. I want to print them both adjacent to each other, while keeping them both in single lines. I used "\r" for both of them, but without an endl the 2nd string prints while the 1st doesn't.
cout << "\r" << row;
track[pos] = '^';
cout << "\r" << track;
It's in a for loop, so if I type an endl after the first cout, then the 1st string moves on to a new line after every iteration. Is there a way to keep both strings in their own lines?
Both strings are printed, but the '\r' character is the carriage return character, which puts the cursor on the first position of the line. That means the second output will overwrite the first output.
Simply print a space between the two strings and it should work:
cout << "\r" << row;
track[pos] = '^';
cout << ' ' << track;
// ^^^
// Print a single space
If the goal is to print the output in two columns and then print a newline, you should do that instead:
cout << row;
track[pos] = '^';
cout << ' ' << track << '\n';

How to count lengths of contents of an array of pointers [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
Is it possible to count the length of the contents of an array of pointers:
For example
char *str[MAX] = {"kendrick", "lamar"};. the length of str[0] is 8 and str[1] is 5. Is there a way of getting these values.
You can verify if each pointer is NULL or not:
int count = 0;
for(int i = 0; i < MAX; i++)
if(str[i] != NULL)
count++;
printf("Count: %d\n", count);
As noted by others, it is not clear what you are exactly asking about.
In a hurry, I wrote the following code to give multiple outputs so you may find what you are looking for:
1- Length of each item by using strlen() function.(ilength)
2- Total length of the items by adding up individual lengths. (ilength).
3- Number of items in the array by counting non NULL (items).
int main()
{
const int MAX = 10;
char* str[MAX] = {"Hello", " World","!",""};
int ilength, length = 0, items = 0;
for(int i = 0; i < MAX; ++i)
if (str[i] != NULL)
{ ilength = strlen (str[i]);
// WARNING!!! ISO C++ forbids converting a string constant to 'char*'
cout << i << " item length is " << ilength << "\n";
length = length + ilength;
items += 1;
}
cout << "\nTotal length of all items is: " << length << "\n\n";
cout << "\nThere is/are " << items << " in the array.\n\n";
}
NOTE: If you use C++ compiler, pay attention to the warning:
WARNING!!! ISO C++ forbids converting a string constant to 'char*'
If you know the strings are all null-terminated, you can use strlen.
Your question is a bit confusing as it is not clear what length you want. Therefore for both possibilities:
Length of array in use:
char *str[MAX] = {"kendrick", "lamar"}; is an array of size MAX with two entries, which are pointers to strings. The compiler will initialize all other entries to zero, so counting how many entries of str are non-zero will give you that answer.
Length of the strings:
A string in C is null terminated. So for each entry of str that is not null you can advance and advance until you encounter a null character. That tells you the length of that entry.

Queries on vectors in c++ [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 5 years ago.
Improve this question
int main() { vector g1; vector :: iterator i; vector :: reverse_iterator ir;
for (int i = 1; i <= 5; i++)
g1.push_back(i);
cout << "Output of begin and end\t:\t";
for (i = g1.begin(); i != g1.end(); ++i)
cout << *i << '\t';
cout << endl << endl;
cout << "Output of rbegin and rend\t:\t";
for (ir = g1.rbegin(); ir != g1.rend(); ++ir)
cout << '\t' << *ir;
return 0;
}
Here in this code variable "i" has been declared as a iterator as well as a variable inside a for loop. isn't that a error?
If we see the first for loop it say that the loop will run till i!=g1.end() that means that the value of *(g1.end()) should not be displayed by *i but it is giving. ide shows output 1 2 3 4 5 for me it should be 1 2 3 4.
i is defined as an iterator in the argument list. When you redefine it in the first for loop, this is a new definition only for the scope of the loop -- this is perfectly legal, though not good practice.
vector::end() points to memory after the final item, not to the final item. So, yes, the final item in the vector will be printed.

how to enter 2 chars in one comand in C++ [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 8 years ago.
Improve this question
int main()
{
long long x,y,z,result;
char f,g;
cin >>x>>y>>z;
**result** =
cout << result ;
return 0;
}
How to make result = x (+ or - or / or *) y (+ or - or / or *) z !?
Reading the operators in between the numbers is simple:
long long x,y,z;
char f,g;
cin >>x>>f>>y>>g>>z;
// See what you've got
cout << x << " " << f << endl;
cout << y << " " << g << endl;
cout << z endl;
However, figuring out the result of the operation is trickier: you need to check the values you've got in f and g, and perform the operations as needed. Note that there must be no space between your numbers and the operators, otherwise the input would be processed incorrectly.
Demo.
This is probably at the core of the exercise that you are solving, so I will suggest that you write a function like this:
long long compute(long long a, long long b, char op) {
... // Check the operator, and return the result
}
With this function in hand, you can produce the result in one simple call:
long long result = compute(compute(x, y, f), z, g);
Once you write the compute function, this should give the result that you expect.
You can do cin>>astring. And separate the string by delimiter and convert them to integer.
For example:
1,2,3
will become '1','2','3'.

Unable to understand struct to char* conversion [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 9 years ago.
Improve this question
I have defined a struct data and assigning it to a char*. However the size of strlen function always gives result as 1 and cout doesn't show anything. Here is my code:
struct data
{
int x;
int y;
};
int main()
{
data d;
d.x = 10;
d.y = 20;
char *ch = (char*)&d;
cout <<"Length:" strlen(ch) << endl;
cout << ch << endl;
}
The output is:
Length: 1
Please help me in understanding what's going on ?
The strlen function counts all bytes in the passed string until it finds the character '\0'. This character is the same as the integer 0.
So what the call to strlen is count the number of bytes until it finds a zero, and in your case the zero happens to be in the second byte of the structures binary representation, meaning that the "string length" is one.
The only thing that you can deduce from this is that you are on a little endian system. Other than that, the call is undefined behavior as the "string" isn't actually a string.
The reason it returns 1 is because you are treating the struct as a char array. Doing this you are re-interpreting the contents of the struct - the integer 10, which is probably stored in memory as 0x0A000000 - as a string. And that yields a length of 1 (only 1 non-zero value before a null-character in the array)
Even though you're trying to treat something as a string which isn't a string, if all you want to do is print out the values in the struct, you can do something like:
struct data
{
int x;
int y;
};
int main()
{
data d;
d.x = 10;
d.y = 20;
cout << "x is " << d.x << ", y is " << d.y << endl;
}
Why are you trying to treat that struct as a string?