How to write character to an array of pointers in C? [closed] - c

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 2 years ago.
Improve this question
Hello this might be asked before, but I couldn't find an answer to my problem.
for (int i = 0; i < StrLenght - 1; i++) {
if (szPattern[i] == '%') {
// DO stuff....
} else {
szBuffer[i] = szPattern[i];
}
}
So, before the if statement everything seems to be alright, I mean szPattern is correctly written to szBuffer, but once I go into the if statement nothing is written in the szBuffer....
I suspect it is because of the null terminated string "\0"..
Any idea how I can solve this problem??
Thanks.. :)

You need
A separate index for writing to szBuffer
Code that adds a zero termination to szBuffer
Like:
int j = 0; // Index for storing in szBuffer
for (int i = 0; i < StrLenght - 1; i++) {
if (szPattern[i] == '%') {
// DO stuff....
} else {
szBuffer[j] = szPattern[i]; // Assign using index j
++j; // Increment index
}
}
szBuffer[j] = '\0'; // Zero terminate
puts(szBuffer);

Related

What is the right logic for a function that compares one value against all elements of an array? [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 10 months ago.
Improve this question
I am writing a CRUD program in C and at one point the user is asked to input an employee's code, which was previously stored in an array called "codes" in an employee registration section of the program, in order to register an employee's vehicle. The program should only continue if the user input is identical to an element of the codes array. Both user input and elements of the array are properly formatted, so there should be no differences other than the actual code.
I debugged by trying the value 1234 for the employee's code, which is successfully stored in the codes array, then I tried registering a vehicle by typing 1234, but the function I wrote that does the checking always return 0, so I'm thinking there is a flaw in the logic.
Variables:
char codes[100][256];
char owner[256];
Function to check if user input is indeed in any element of the array codes, returns 1 if it finds a match, or 0 at the end.
int checkOwner(char t_code[])
{
for (int i = 0; i < indice; ++i)
{
if(!strcmp(t_code, codes[i])) {
return 1;
}
}
return 0;
}
Input processing:
do {
printf("Type owner's code: ");
fgets(owner, sizeof(owner), stdin);
owner[strcspn(owner, "\n")] = '\0';
if(!checkOwner(owner))
printf("No employee with that code, try again.\n");
} while(!checkOwner(owner));
I think the "logic flaw" stands in the for statement:
for (int i = 0; i < indice; ++i)
From the logic I see you want to go through all the codes you actually registered. However, at each iteration you would like to go to the next "line", but you only update i by one.
for (int i = 0; i < indice; i+=256)
should solve the problem hopefully, though I think at this point the condition i < indice will cause problems (you could then use a second variable to count the "line number")
for (int i = 0, j = 0; j < indice; i+=256, ++j)
I would also explicitly say:
if(strcmp(t_code, codes[i]) == 0)

Comparing hashses in C [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 4 years ago.
Improve this question
Hi I am comparing the hashes and I can't get the correct output. So please can you guys help me out.
for(int i=0;i<len;i++)
{
if(hash1[i]==hash2[i])
{
return 1;
break;
}
else
return 0;
break;
}
You are using the return keyword to early, once the code hits a return, it goes out of the for-loop. So what you are doing there is comparing just the first element of both hashes.
The break statement also breaks the loop cycle, but in your code it never actually reaches this statement, because there is always a return before.
You should probably try something like:
for (int i=0; i<len; i++) {
if (hash1[i] != hash2[i]) {
return 0;
}
}
return 1;

What is the meaning of "C{0}\t" in flowchart? (flowchart in unknown programming language, for implementation in C) [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 5 years ago.
Improve this question
I'm writing flowcharts in C. And I've come across with the printable value "C{0}\t". It's in the fifth box.
I got the flowchart from the internet. So I don't have means to ask the person who made it for additional information. I think that the flowchart is not intended to be used in a C programming class, but in another language, which I don't know.
Any idea what is the meaning? How could I translate it into C?
Do you know what language is? I can find also in the same exercise sheet things like these:
i<array.GetLength(1)
or
DisplayLastWithColSums(int[,] array)
Thanks in advance.
I coded the following solution:
void main ()
{
int i = 0, j = 0, k = 0, col = 10;
char sum_dashes[1000] ;
printf("\n\t");
while (i < col)
{
printf("C%i\t", i + 1);
i++;
while (j < 8)
{
sum_dashes[k] = '-';
k++;
j++;
}
j = 0;
}
printf("\n\t");
for (i = 0; i < k; i++)
{
printf("%c", sum_dashes[i]);
}
}
That prints out the next result:
C1 C2 C3 C4 C5 C6
------------------------------------
I think {0} is intended as a placeholder for the 0th argument after the quotes. So..
"C{0}\t", i+1
Produces “C1” then “C2” then “C3” etc separated by tab characters (\t)
The equivalent in C would be:
printf("C%d\t", i+1);

Return a certain value based on the contents of a string [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 5 years ago.
Improve this question
For example if c[20] = "Six of Spades", then 6 is returned, if c[20] = "Nine of Hearts", then 9 is returned, and so on. So the value returned is based only on the first word in the string. Is there a simple method to do such thing?
int cardValue(char* card)
{
char* values[] = {"One", "Two", "Three" ... };
for(int i = 0; i < sizeof(values) / sizeof(values[0]); i++)
{
size_t len = strlen(values[i]);
if (strncmp(values[i], card, len) == 0)
{
return i + 1;
}
}
return -1; // error
}
You may need some special case handling when you get to Ace, King, Queen etc.
No
You can implement it yourself though, by fishing the first word from the string (e.g. with strtok()), and then based on that substring, return the corresponding number.

Address is not incrementing? [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
It is said that,in c,b++; is equal to b=b+1; if this is the fact test++ in my code why generate a compile time error.
test+1 is working well but test++ is not working.but why?
#include<stdio.h>
int main(void)
{
char test[80]="This is a test";
int a=13;
for(;a>=0;a--)
{
printf("%c",*(test++);
}
}
The ++ and -- operators are not defined for arrays.
v++; would be the same as v = v + 1;. Assumed v was typed an array this would imply assigning to an array, which is not defined.
char test[80] = "This is a test";
char *p = test;
for(int a = 0; a < 14; a++)
{
printf("%c", *(p++));
}
Well, for one thing, b++ is not the same as b=b+1.
But even if it were -- I think you'll find you get a similar error if you try test = test + 1.

Resources