Expression syntax error issue [closed] - c

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 was writing a simple code to change from lowercase to uppercase but its showing expression syntax error on a line 12 in the if() statement. The code runs fine without equality sign but does not work with equality. Any help to rectify it is appreciated.
The code is as follows:
#include<stdio.h>
#include<string.h>
#include<conio.h>
main()
{
char s[]="Computer";
int i;
clrscr();
for (i=0;i<strlen(s);i++)
{
if (s[i] > = 'a' && s[i] < = 'z')
s[i]+='A'-'a';
}
puts(s);
getch();
return 0;
}

This > = is not valid syntax because of the space between > and =. Delete the space :
if (s[i] >= 'a' && s[i] <= 'z')

The syntax error you are getting is due to the space between > and = operators. Likewise for < and =. Also, this would be a better version of your code:
int main()
{
char s[] = "Computer";
size_t i, len = strlen(s);
for (i = 0; i < len; i++) {
if (s[i] >= 'a' && s[i] <= 'z')
s[i] -= 32;
}
puts(s);
getch();
return 0;
}

Please look your code in if(s[i] > = 'a' && s[i] < = 'z')
as if(s[i] >= 'a' && s[i] <= 'z')

Related

C programming count number of occurrences? [closed]

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 working though the C programming book. I have a question in the arrays section (1.6) that says "Let us write a program to count the number of occurrences of each digit, of white space characters (blank, tab, newline) and all other characters.
#include <stdio.h>
/* count digits, white space, others */
int main()
{
int c, i, nwhite, nother;
int ndigit[10];
nwhite = nother = 0;
for (i = 0; i < 10; i++)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-0];
else if (c == ' ' || c == '\n' || c == '\t')
nwhite++;
else
nother++;
printf("digits =");
for (i = 0; i < 10; i++);
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n", nwhite, nother);
}
This is the compiling code. If I type 23, enter, 23, enter then ctrl+d it returns "digits = 1044509184, white space = 2, other = 0". Why? What is digits doing?
if (c >= '0' && c <= '9')
++ndigit[c-0];
This block of code is problematic as it is accessing the array out of bounds (and will cause undefined behaviours).
It should be
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
Notice the difference between 0 and '0'. The latter piece of code works as it is guaranteed that number characters are encoded consecutively.

How to change numbers in an array to symbols

I am trying to find a good way to convert digits of a string in an array to a # symbol.
I would like to be clear, this is for a homework assignment. I am asking for help because not only am I running into roadblocks that I would like to overcome; but I want to understand the relationship between arrays and functions as well.
I have done some code, shown below, but I feel like I am confusing myself on the relationship between calls in arrays when they are within a function.
Here is what I have done:
void coverNumbers(char s[]) {
char num;
if (s[num] >= '0' || s[num] <= '9')
num = '#';
}
I feel like I am so close to having a solution.
If I type in 43ll0 within the array, I want the 43ll0 to become ##ll#.
Thank you for your help everyone, and have a wonderful night!
You are going about this the wrong way. You need to loop over the string to check and replace each character individually.
Try This
void coverNumbers(char s[]) {
for (int i = 0; s[i]; i++)
if (s[i] >= '0' && s[i] <= '9')
s[i] = '#';
}
You have additional logic mistakes. If you have some questions on why I changed a specific thing please ask in the comments.
Assuming covernumbers receives a C string, ie a pointer to a char array terminated by a null byte '\0', you should just iterate on all characters and perform the modification. Note however that you used || instead of && to test for digits. s[num] is a digit if it is >= '0' and <= '9'.
Here is a corrected version:
void coverNumbers(char s[]) {
for (size_t i = 0; s[i] != '\0'; i++) {
if (s[i] >= '0' && s[i] <= '9')
s[i] = '#';
}
}
You can also use the pointer and increment it. void coverNumbers(char s[]) and void coverNumbers(char *s) are equivalent and the second version make it more obvious that s is a pointer, not an actual array.
void coverNumbers(char *s) {
while (*s) {
if (*s >= '0' && *s <= '9')
*s = '#';
s++;
}
}
Welcome to Stack Overflow!
You should not use stack overflow for such kind of problems. But you can always ask questions on the issues you are facing, if the similar question is not already been asked by others.
SOLUTION:
Algorithm:
Get the length of the string
Iterate each character in the string
Based on the value in current position (index) of string, replace with '#' if it's a digit.
Print the output
Code:
void coverNumbers(char s[]) {
int len = strlen(s);
for (int i = 0; i < len; i++)
if (s[i] >= '0' && s[i] <= '9')
s[i] = '#';
printf("%s", s);
}

Lvalue required: error in c [closed]

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
if(puzzle[i][j] >= '0' && puzzle[i][j] <= '8' && puzzle[i][j] = '.')
I have that code and when I compile it, it gets Lvalue required error. Help. Thanks!
here is the full code. What I'm trying to do is to check the input of the user with the guidelines:
1) The input should be 0 to 8 and '.'
2) In the corners of the table( since it is a 2d array, it should be less than 3.
int main()
{
char puzzle[7][7];
int i;
int j;
printf("Enter your Tentaizu Puzzle:\n");
for(i = 0; i < 7; i++){
for(j = 0; j <7; j++){
scanf("%s", &puzzle[i][j]);
}
}
if(puzzle[i][j] >= '0' && puzzle[i][j] <= '8' && puzzle[i][j] == '.'){
if((puzzle[0][0] <= '3' && puzzle[0][6] <= '3') || (puzzle[6][0] <='3' && puzzle[6][6] <= '3')){
printf("Tentaizu Puzzle\n");
for(i = 0; i < 7; i++){
for(j = 0; j <7; j++){
printf("%s\n", puzzle[i][j]);
}
}
}
}
getch();
}
You typed = where you meant ==. As a result, C is trying to assign '.' to the result of puzzle[i][j] >= '0' && puzzle[i][j] <= '8' && puzzle[i][j] -- and you can't assign a value to a (boolean, in this case) value.

rubbish instead of string display [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
The task is to be sure that phrases are can be readed vise versa or not (without spaces ' ').
So I got the point and made some code. But when I made
printf("%s", str_1)
I found a lot of same russian letters.
For example my inputs are: '123' '321', but displayed rubbish
My str_1[i] is going to be like 123321 and then checked first and last and so on members.
After that I added in if condition if(str[i] != 'М') and now everything nice.
Please tell me what I did wrong and what is going on with "M".
I hope my question is clear.
#include "stdio.h"
#include "locale.h"
int main(){
char str[100];
char str_1[100];
int i, j, k, l;
j = 0;
l = 0;
fgets(str, 100, stdin); //we got array
//how many chars before '\0'
for(i = 0; i < 100; i++)
{
if(str[i] != ' ' && str[i] != '\n' && str[i] != 'М')
{
str_1[j] = str[i]; //made new array without spaces between words or whatever
j++;
}
}
j--;
for(k = 0; k < j; k++)
{
if(str_1[k] == str_1[j - 1 - k])
{
l++;
}
}
if( l == k)
{
printf(";) YES! good job!\n");
}
else
{
printf(";) NOT! I'm sorry!\n");
}
return 0;
}
As it seems, you skip over the end of the string, as you never check where it ends.
At least, this is my suspicion; I am not sure if it is the right reason.
You should check for str[i] == '\0' as well and then break the whole loop.
As I see, you even made this consideration in
//how many chars before '\0'
but didn't include it in code.
A
for(i = 0; i < 100 && str[i] != '\0'; i++)
should do the trick if you omit the j-- afterwards.

I dont know what is wrong with this program. it's supposed to convert binary to decimal in c [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
well this program is supposed to convert a binary number to decimal. im new to c and im not very confident in coding C. tried and coded this program but doesnt run properly. and I have no idea what part is wrong here. need some help to complete this program.
thanks
binaryToDecimal(char str[]) {
for(int i = strlen(str)-i; i>=0; i--) {
if(str[i] == 48 || str[i] == 49) {
int tmp = pow(2, counter);
int sum= str[i]*tmp;
counter++;
answer += sum;
} else if(str[i]>49 || str[i]<48) {
printf("error");
}
}
printf("%d", &answer);
}
This phrase doesn't make sense:
int i = strlen(str)-i;
i hasn't been initialized yet, but you're using it in the expression!
I believe you meant:
int i = strlen(str)-1; // ONE ... not I
str[i] is a character, either '1' or '0' which as you have it is equal to 48 and 49 as integers. So you want to convert them to 1 and 0 them do the multiplication.
sum = (str[i] - 48) * tmp;
Assuming all variables have been initialised to sensible values and str holds the input. The following code should work, although it is not particularly efficient.
You can use '0' and '1' instead of more obscure 48 and 49.
for (i = strlen(str)-1, j = 0; i >= 0; --i, ++j) {
if (str[i] != '0' && str[i] != '1') {
printf("ERROR: unxepected input\n");
exit(EXIT_FAILURE);
}
if (str[i] == '1')
sum += pow(2, j);
}
when printing the result provide the value and not it's address, e.g.
printf("%d\n", sum); // (!) not &sum

Resources