Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm doing the CS50 course, pset2 initials. The only error I get is a problem with get_string: it would have an 'incompatible pointer types initializing 'string' with an expression of type 'string (void)'. I really don't understand what I am doing wrong, because my code for get_string worked for the last problem set.. Here's my code:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
// ask user for input
printf("What are your names?");
string s = get_string;
// print first letter from string & capitalize
printf("%c", toupper(s[0]));
//iterate over characters in current string + start loop
for (int i = 0; i < strlen(s); i++)
{
//find space character
if (s[i] == ' ')
{
// print character next to space & capitalize
printf("%c", toupper(s[i++]));
}
// new rule
printf("\n");
}
}
Although you have not shown us cs50.h, we can guess that get_string is a function, so this:
string s = get_string;
Needs to be:
string s = get_string();
get_string is a function that takes no parameters:
string get_string(void);
so
string s = get_string;
must be
string s = get_string();
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am new to C can anyone help me how to fix this code.
I am getting a error:
'else' without a previous if
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20];
char str2[20];
int value;
printf("Enter the string: ");
scanf("%s",str1);
printf("Enter the string: ");
scanf("%s",str2);
value=strcmp(str1,str2);
if (value==0);
printf("The strings are same");
else
{
printf("The strings are not same");
}
return 0;
}
The problem here is that the semicolon after the if statement
if (value==0);
evaluates to: if value is equal to 0, do nothing.
The following line
printf("The strings are same");
not only would get executed every time, but also breaks the if-else apart so that the following else statement does not find any related if statement.
You could use braces here instead to prevent such problems in the future.
if (value == 0) {
printf("The strings are same");
} else {
printf("The strings are not same");
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
char hi[10] = "bye";
char a = 'a';
strcat(hi, a);
Like the example above. How would I do this in C? Is there a more general string I cant let hi be?
a is a char type while strcat expects it's both arguments of type char *. To append the char to an array of characters you can do this
int index = strlen(hi);
if(index < sizeof(hi)-1)
hi[index] = a;
Note that in this particular case the initializer will initialize the first three elements of hi to b, y and e respectively. The rest of the elements will be initialized to 0. Therefore you do not need to take care of the null termination of the array after appending each character to it. But in general you have to take care of that.
int index = strlen(hi);
if(index < sizeof(hi)-1){
hi[index] = a;
hi[index+1] = '\0';
}
strcat(hi, (char[]){a,0});
This would append the a.
Or you can do this
char s[]={a,0};
strcat(hi,s);
Or simply
#define MAXLEN 10
...
size_t len = strlen(hi);
if( len+1 <= MAXLEN-1)
hi[len]=a,hi[len+1]=0;
else
// throw error.
In your case hi[len+1]=0 is not required as it is already filled with \0. Also as mentioned by Serge that you can use simply used the string literal as the second parameter to the strcat function.
strcat(hi,"a");
There is a subtle difference in this two as mentioned by Serge again, that string literals are const but the compound literals are not.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am a beginner C-programmer. Recently I've been trying to practise using string functions in C.
As such, I wrote the following program:
MessageDetector.c
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[100] = "Alex:HeyGoodMorning!:1911hrs:0:1012:2017:::";
char *p = strtok(a,":");
char n[20];
int i = 1;
while(p != NULL) {
strcpy(n,p);
p = strtok(NULL,":"); //limit to characters before semi-colon
i++;
if (i = 2) { //after 2 occurrences of the semi-colon. print a string
printf("%s\n",n);
break;
}
}
return 0;
}
The output of my program is as follows:
Alex
However, I would like the program to output
HeyGoodMorning!
What are the changes I should make to the above program? Your help is greatfully appreciated
Initialize the variable i with 0 and use comparison instead of assignment in this condition
int i = 0;
//...
if(i == 2){//
Take into account that the first call of strcpy is redundant.
In fact you could do the same without a loop. For example
char a[100] = "Alex:HeyGoodMorning!:1911hrs:0:1012:2017:::";
char *p;
if ((p = strtok(a, ":")) && (p = strtok(NULL, ":")))
{
puts(p);
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I need to fill manually an array of chars. I declared it like this:
char* coor = malloc(sizeof(char) * 5);
Then I manually assigned every variable to its position:
coor[0] = O_colum;
coor[2] = ((char)(O_row+48));
coor[3] = '-';
coor[4] = D_colum;
coor[5] = ((char)(D_row+48));
(D_Row and O_row are integers, I need that number in character form, not the equivalent value in ASCII; that’s why I do +48)
The problem comes when I try to print it. If I use printf(" %s", coor) it only prints the first characters and I don’t know why. I’m using %s, so it should print all the characters in the string.
When I do this:
char *p = "hello";
printf("%s",p);
It does print hello.
There are two mistakes in your code:
you are skipping the position 1 of the array. This is probably the
reason why it prints only the first element.
you need to add the end string character \0 in the end of the
string.
This should fix it :
char* coor = malloc(sizeof(char) * 6);
coor[0] = O_colum;
coor[1] = ((char)(O_row+48));
coor[2] = '-';
coor[3] = D_colum;
coor[4] = ((char)(D_row+48));
copr[5] = '\0';
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm struggling to get Char to work. It keeps returning an error.
#include <stdio.h>
#include <cs50.h>
int main (void)
{
int tower_height;
char #;
// Inputs
do {
printf("Let's build! Give me a number between 0 and 23 'inclusive'.\n");
tower_height = GetInt();
}
while
(tower_height < 0 || tower_height > 23);
// Outputs
for (tower_height = 0; tower_height <= 23; tower_height++)
printf ("%c = tower_height - 2\n");
}
C identifier names may contain letters, underscore, and digits, as long
as the first character isn't a digit, and as long as the identifier
isn't a keyword. They may not contain #.
# is not a valid variable name.
As pointed out, # is not a valid variable name.
You can see how # is properly used in the first line of your code: #include <stdio.h>
Instead, call your char variable something that uses letters and numbers eg: char c;