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 am comparing two strings each coming from arrays.
while(countX<10){
if(strcmp(scanWord[countX], currentWord[countX]) == 0)
{scoreCurrent++;scoreCurrent++;}
countX++;
}
"scanWord[countX]" and "currentWord[countX]" don't compare; each time coming up that they aren't the same even if they are. It works if I compare things that aren't those and I have printed them to screen to check too. They just don't seem to play well. Thanks in advance.
When you're reading the line, remove the newline:
char *line = fgets(currentWord[countX], 20, stdin);
if (line) {
int len = strlen(line);
if (line[len-1] == '\n') {
line[len-1] = 0;
}
}
The question as I see it:
"scanWord[countX]" and "currentWord[countX]" don't compare; each time coming up that they aren't the same even if they are.
How do you KNOW the two values are the same? Print them out. Print out the length too. If dealing with Unicode of any sort, print out the individual bytes in hexadecimal, because two Unicode characters that look identical may actually be different.
Because the computer will never say the strings are different if they are really the same. The answer to the question as I read it is that the strings actually are different.
Related
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 3 days ago.
Improve this question
I am doing the CS50 problem set 2 wordle and I am stuck with the TODO: 5.
I have two words of the same length: the "guess" from the user and the "word", which they try to guess.
I am supposed to check if some letters of the "guess" are in the actual "word" and if they are in the correct spot.
I tried to make an array of characters which is basically the word itself in order to compare each letter of the guess with the actual word but everything I find is just looks like that:
char myString[] = "This is some text";
But the problem is that the words are saved as strings and I cannot do this:
char myString[] = guess;
It would be great if someone could help me with that problem.
char myString[] = "%s", guess;
but that does not work!
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 last year.
Improve this question
I would like to ask you if it was possible to get the characters common to two strings without having to resort to a loop on the character array. I wonder why this could greatly affect the total cost (asymptotically for n-> infinite) of algorithms such as eg. Charm or Eclat (just think that it would be like adding a new cycle to those already present). Thank you.
Specifically, the algorithm I am referring to is the following. As can be seen from the photo (line 6) it is necessary to obtain the intersection by iterating on the indices i and j, so I suppose it is necessary to iterate. I guess I get an O(m + n) best assuming the insert and search operations use O(1).
If your characters are byte-encoded (US-ASCII, KOI8-R, etc), you can create array, where your char is index, and iterate 1st string, and set "1" here. Thereafter, iterate 2nd string, and print only chars, presents in the array. See the example:
void print_intersection(const unsigned char *s1, const unsigned char *s2) {
unsigned char arr[0x100], c;
bzero(arr, sizeof(arr)); // cleanup
while(c = *s1++)
arr[c] = 1;
while(c = *s2++)
if(arr[c] != 0) {
putchar(c);
arr[c] = 0; // Disable print dups
}
}
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 3 years ago.
Improve this question
I was making a script that is calculating the additions between two natural numbers which decimal lengths should be smaller or same with 10000, and printing a result of the sum.
Of course, there ain't any variable type that can hold a integer which length is 10000 in C.
So, I made the program by utilizing the simple additions' calculating logic that all we learn in a school when we were young. And also, I just should use strings to get those gigantic numbers.
But some results were starting with zero. I knew why did the zero appeared there, but I did prefer to have a result that is like "1234", not "01234". By the way, all other stuffs were perfect.
I needed a function that gets input as string, and erases a single zero starts with a string if it exists.
And could you make it instead of me, please? You should probably consider that the strings we will deal with can have such a length that is smaller or same with 10000.
Maybe this:
char * f( char * str )
{
while ( *str == '0' && str[1] )
str++; // skips all zero-s when it is not last character in string
return str;
}
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 5 years ago.
Improve this question
For example it prints '(night' despite tokenizing (), why does this happen?
char* word = strtok(&c, ",.;()");
while(word!= NULL)
{
word = strtok(NULL, ",.;()");
printf("%s ", &c);
}
Your code just prints &c on every iteration (whatever that is). You never print word, which is your next token. That's why you never see the results of your tokenization. If you want to see the tokens, you have to print word, not c.
On top of that it is completely unclear why you are applying & operator to your c. If c is a string pointer or a char array, that & there makes no sense at all.
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 5 years ago.
Improve this question
This function make a strange error after using it several times and I really can't understand the reason behind it.
char *get_range(char *str,int min,int max){
char *_res=(char *)malloc(sizeof(str));
int cur=0;
while (min<max){
_res[cur]=str[min];
min++;
cur++;
}
return _res;
}
The problem is that after using this function several times, the output comes with additional chars and I don't understand why.
Notice: The additional chars are allway used returned by the function beffor
char *_res=(char *)malloc(sizeof(str));
is wrong. sizeof(str) is measuring the size of a char pointer. This is either 4 or 8 (typically) depending on your system (32 or 64 bit).
You need
char *_res=(char *)malloc(strlen(str) + 1);
strlen returns the number of characters in the string, and you need to add 1 for the terminating 0;
Second you have to add a terminating zero at the end, do:
_res[cur] = '\0';
before returning