compare strings with strcmp function works different [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm comparing two strings with strcmp in the following manner:
long t=1011;
char tc[10], tcr[10];
ltoa(t,tc,10);
cout<<tc<<endl; //prints 1011
strcpy(tcr, strrev(tc));
cout<<tcr<<endl; //prints 1101
cout<<strcmp(tc,tcr);
This gives me a result of 0, which indicates that the strings are equal. However, when I try:
cout<<strcmp("1011", "1101"); // prints -1 thats okay
I get the expected value of -1. What's am I doing wrong? I am using devc++ compiler version 4.9.9.2

It depends on how function strrev is defined, If it reverses the argument in place then the result is expected because tc was reversed.
For example function strrev can be declared the following way
char * strrev( char *s );
and the return value and the value of the argument will be equal.
Take into account that strrev is not a standard function.

If you change your code like so:
long t=1011;
char tc[10], tcr[10];
ltoa(t,tc,10);
strcpy(tcr, strrev(tc));
cout<<tc<<endl;
cout<<tcr<<endl;
cout<<strcmp(tc,tcr);
then you'll see that tc and tcr are the same. strrev reverses the input string in place, and 1101 is printed twice.

Related

Getting initializer-string for char array is too long. Is this a compiler error? [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 1 year ago.
Improve this question
static_assert(0<decltype(AType::Id)::MaxIdLen);
using __type = char[decltype(AType::Id):: MaxIdLen + 10000];
[[maybe_unused]] __type aa = ""; //error initializer-string for char array is too long
I am getting this weird compiler error saying the initializer string is too long. But it is actually not. The first static_assert passed.
Has anyone seen such an issue before? I am using clang.
... compiler error saying the initializer string is too long. But it is actually not.
The compiler is not telling you that the string is too long to init aa. It's telling you that it's more data than it can handle, presumably because decltype(AType::Id)::MaxIdLen is a large value.
The compiler doesn't just store the string literal in the character array. It initialises the entire character array by using the string literal as a prefix, and padding the rest with zeros.

C++ : How to arrange 4 values in an ascending order? [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 6 years ago.
Improve this question
Hello I know it may be a beginners question but I need help.
I need to compare between 4 values added by the user and arrange them in am ascending order by using a function that takes 2 inputs and return the smaller one. I know it can be done by arrays but I must not do it. I already have the function but I don't know how to use to do the trick without having a very long code. Thanks
This seems to me to be an obvious "homework question," so let me answer it cryptically in order to maybe push you in the right direction.
First, the hint: divide and conquer.
Second hint: the "Towers of Hanoi" problem.
You have a function that can compare two values. Okay, then: "four elements" can be viewed as "two groups of two values each." Given that either of the two input to your comparison-function can be the result obtained by a nested call to the same function . . . you can, indeed, solve this problem, in one line of code, without using arrays.
I'm trying here to "teach you to fish," so I'm not handing you the fish on a platter.
If you know c++ then you can use sort function. But for this you have to include algorithm as:
#include <algorithm>
and sort function will be used as:
sort(array, array+N);
where array is the array name and N is the size of array.After this operation you will get a sorted array in ascending order and return first element.Now the function will look like as:
int smallest(int *array) {
int size = sizeof(array) / sizeof(array[0]);
sort(array, array+size);
return (array[0]);
}
And now call this function from main()

C++ Function Prototype? [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 7 years ago.
Improve this question
I had this on a test this week and got it wrong. I asked the professor for help and he said we'd go over it next Thursday. I really don't want to wait that long. Can any one on here walk me through it?
Perform the task specified by the following statements:
Write the function prototype for function "doit" that takes an integer array parameter "list" and an integer "size" parameter and returns a boolean value.
Thanks in advance for any help
bool doit(int list[], int size);
The function doit takes an array of integers as first parameter, and the size of the array (which is an integer) as second parameter. It returns a boolean (true or false).
This sort of function prototype is typically used to access each element of the array within a for loop (with size as terminating condition). The boolean return value could inform of the presence or absence of some value in the array for example, or if some work could or could not be performed.
A little bit of help is there :
RETURN_TYPE FUNCTION_NAME (ARGUMENT1_TYPE ARGUMENT1,ARGUMENT2_TYPE ARGUMENT2);
It's not a big deal , it's just a prototype :)
I wrote the answer and removed it, u should try looking for the answer in the internet :)
Here you go:
+ doit(list : int[], size : int) : boolean

Odd characters in output [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm trying to write a simple hangman game in c++ by randomly selecting a word from a list, checking the string length, and writing that many *s into a new string to serve as placeholders in the yet un-guessed word. The max length is 9 letters. I have the game working almost flawlessly -- the problem is that whenever my word has 8 or 9 letters, the program prints the correct number of *s followed by one or two � characters. Research tells me these are unprintable characters, but I've tried for a while now and I'm not sure why they're here, why they only show up with a word length>7, or how to get rid of them. Below is relevant code. Any suggestions?
Generating *s:
char word[80];
int len=strlen(targetWord);
for(int i=0;i<len;i++){
word[i]='*';
}
You forgot to add the \0 terminator at the end of the string. After the for loop, add:
word[i] = '\0';
Or, best, use std::string instead of a C string.
Try using std::string instead.
std::string word;
int len=strlen(targetWord);
for(int i=0;i<len;i++){
word+='*';
}

How to iterate through all utf-8 code points in a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to be able to iterate through all of the utf-8 characters in a string.
Imagine that all code points had a index, the first code point had index 0 and the last code point had index N. I want the iterator to be able to jump X indexes forward and also tell me the index of a code point.
I want to do something similar to http://www.nubaria.com/en/blog/?p=371, but I am not sure how to iterate over the bytes so that the iterators always refer to the start of a legal utf-8 code points.
Warning: this method only works if you already know that your bytes contain clean UTF-8. It will not work properly in the presence of malformed or invalid characters.
The second through last bytes of a UTF-8 encoded codepoint will always have a bit sequence of 10xxxxxx. Skip over those and you'll be at the start of the next codepoint.
for (int i=0; i<X && *p!=0; ++i)
{
++p;
while ((*p & 0xc0) == 0x80) ++p;
}
The *p!=0 is there to make sure you don't run past the end of the string.