Error: expected expression in else if statement - c

This is the error I am getting:
test.c:110:21: error: expected expression
else if(isupper(p_i))
^
1 error generated.
In an else if statement towards the end of the code—"else if(isupper(p_i))"—the error is generated.
I have commented above this 'else if' statement. Please let me know what's wrong. Thank you.
#include <stdlib.h> // The library which contains the 'atoi()' function
#include <stdio.h> //
#include <cs50.h> // typedef char *string; and GetString()
#include <string.h> //
// 'argv[]' is an array of strings. (Fun fact: A string is an array of characters.)
// 'argc' is the integer variable which stores the number of strings that are in 'argv[]'.
int main(int argc, string argv[])
{
// VARIABLE DECLARATIONS
int k; // Integer variable for the non-negative, encryption key
string plaintext; // Variable to store the information to be encrypted
int n; // Integer variable for the string length
string ciphertext = NULL; // Variable to store the encrypted information
// This loop analyzes the command-line argument(s): We need exactly one argument (i.e. argc = 2)
if (argc > 2)
{
printf("\n");
printf("Too many arguments. Please try again.\n");
return 1;
}
else if (argc < 2)
{
printf("\n");
printf("This program requires that you provide an argument. Please try again.\n");
return 1;
}
else if (argc == 2)
{
k = atoi(argv[1]);
if (k == 0 || k < 0)
{
printf("\n");
printf("Invalid input: encryption key needs to be a non-negative integer.\n");
return 1;
}
}
// Prompt the user for a string input to be encrypted:
printf("\n");
printf("Please enter the information to be encrypted:\n");
plaintext = GetString();
n = strlen(plaintext);
printf("n = %d \n", n);
// We need to implement Caesar's Cipher algorithm:
// But first, we select for alphabets only by using the 'isalpha()' function:
for (int i = 0; i < n; i++)
{
int p_i = plaintext[i];
int isalpha(int p_i);
if (isalpha(p_i))
{
int islower(int p_i);
if (islower(p_i))
{
printf("Caesar's algorithm for the lower case goes here.\n");
}
int isupper(int p_i);
//-----------------------------------------------------------
// THE FOLLOWING else if STATEMENT IS THE SOURCE OF THE ERROR
//-----------------------------------------------------------
else if(isupper(p_i))
{
printf("Caesar's algorithm for the upper case goes here. \n");
}
}
else
{
for (int j = 0; j < n; j++)
ciphertext[i] = plaintext[i];
}
}
// Program terminates
return 0;
}

You have a function prototype between the if and the else:
if (islower(p_i))
{
printf("Caesar's algorithm for the lower case goes here.\n");
}
int isupper(int p_i);
//-----------------------------------------------------------
// THE FOLLOWING else if STATEMENT IS THE SOURCE OF THE ERROR
//-----------------------------------------------------------
else if(isupper(p_i))\
The else block must follow immediately after the if block. If you had put the line int isupper(int p_i); pretty much anywhere else (before the first time you use it), you would not have this error. Even better, you should load this prototype via #include <ctype.h> at the top of your file.

int isupper(int p_i);
//-----------------------------------------------------------
// THE FOLLOWING else if STATEMENT IS THE SOURCE OF THE ERROR
//-----------------------------------------------------------
else if(isupper(p_i))
Remove this declaration: int isupper(int p_i);.
Use the correct #include directive at the top of your source file to declare isupper function:
#include <ctype.h>

You cannot use any statement between if and else statement. Example -
if()
{
//some code
}
//some Code ---> This is wrong //this should not be in between if and else
else
{
//some code
}

Your function prototypes:
int islower(int p_i);
int isupper(int p_i);
do not belong within the main() function (or any function) - though that's technically legal which is why the first is not causing a problem.
However, they cannot live within an 'if / else' construct - you have the second sandwiched between an if clause and an else clause.
The best solution is to put them at the head of the file, or better just use:
#include <ctype.h>
to get the relevant header file with the prototypes in included, then remove the prototypes from the function.

Related

How to find duplicate letter in array in C

I am making a program which requires the user to input an argument (argv[1]) where the argument is every letter of the alphabet rearranged however the user likes it. Examples of valid input is "YTNSHKVEFXRBAUQZCLWDMIPGJO" and "JTREKYAVOGDXPSNCUIZLFBMWHQ". Examples of invalid input would then be "VCHPRZGJVTLSKFBDQWAXEUYMOI" and "ABCDEFGHIJKLMNOPQRSTUYYYYY" since there are duplicates of 'V' and 'Y' in the respective examples.
What I know so far is, that you can loop through the whole argument like the following
for (int j = 0, n = strlen(argv[1]); j < n; j++)
{
//Place something in here...
}
However, I do not quite know if this would be the right way to go when looking for duplicates? Furthermore, I want the answer to be as simple as possible, right know time and cpu usage is not a priority, so "the best" algorithm is not necessarily the one I am looking for.
Try it.
#include <stdio.h>
#include <stddef.h>
int main()
{
char * input = "ABCC";
/*
*For any character, its value must be locate in 0 ~ 255, so we just
*need to check counter of corresponding index whether greater than zero.
*/
size_t ascii[256] = {0, };
char * cursor = input;
char c = '\0';
while((c=*cursor++))
{
if(ascii[c] == 0)
++ascii[c];
else
{
printf("Find %c has existed.\n", c);
break;
}
}
return 0;
}
assuming that all your letters are capital letters you can use a hash table to make this algorithm work in O(n) time complexity.
#include<stdio.h>
#include<string.h>
int main(int argc, char** argv){
int arr[50]={};
for (int j = 0, n = strlen(argv[1]); j < n; j++)
{
arr[argv[1][j]-'A']++;
}
printf("duplicate letters: ");
for(int i=0;i<'Z'-'A'+1;i++){
if(arr[i]>=2)printf("%c ",i+'A');
}
}
here we make an array arr initialized to zeros. this array will keep count of the occurrences of every letter.
and then we look for letters that appeared 2 or more times those are the duplicated letters.
Also using that same array you can check if all the letters occured at least once to check if it is a permutation
I don't know if this is the type of code that you are looking for, but here's what I did. It looks for the duplicates in the given set of strings.
#include <stdio.h>
#include <stdlib.h>
#define max 50
int main() {
char stringArg[max];
int dupliCount = 0;
printf("Enter A string: ");
scanf("%s",stringArg);
system("cls");
int length = strlen(stringArg);
for(int i=0; i<length; i++){
for(int j=i+1; j<length; j++){
if(stringArg[i] == stringArg[j]){
dupliCount +=1;
}
}
}
if(dupliCount > 0)
printf("Invalid Input");
printf("Valid Input");
}
This code snippet is used to count the duplicate letters in the array.
If you want to remove the letters, Also this code snippet is helpful .Set null when the same characters are in the same letter.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int count=0;
int array[]={'e','d','w','f','b','e'};
for(int i=0;i<array.Lenth;i++)
{
for(int j=1;j<array.Length;j++)
{
if(array[i]==array[j])
{
count++;
}
}
}
printf("The dublicate letter count is : %d",count);
}

new to c problem with simple game problem with function

im new to c i try to make a little and very simple game of hangedman and i dont know why doesent work get error in gcc "expected declaration or statement at the end of input"
im new to c and ii try very hard to learn it.
im missing something? my function is not right? some advice to learn alghorytmically thinking?
thanx in advance for the hel you gonna give me
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//function to find letter in string
int findletter(char y)
{
char c;
int i;
char secret[] = "outcast";
i = 0;
scanf("%c", &c);
while (c != secret[i] && i < strlen(secret))
i++;
if(c == secret[i])
return (1);
else
return (0);
}
//confirmation letter
int guessed(char a)
{
int z;
char guess [6] = {0};
z = 0;
while(findletter(guess[z]) != 1 && findletter(guess[z]) < 6)
{
z++;
if(findletter(guess[z]) == 1)
return 1;
else
return 0;
//word guessed
int tryguess(char v)
{
int x;
x = 0;
while(findletter(guess[x]) == 0)
{
x++;
if(findletter(guess[x] == 1))
return 1;
else
return 0;
}
}
int main()
{
char secret[] = "outcast";
char letter;
int lives;
char guess [6] = {0};
int i;
lives = 10;
i = 0;
printf("welcome to the hanged man\n");
while(i < 6)
{
if((findletter(secret[i] == 1)))
printf("%c", secret[i]);
else
printf("*\n");
i++;
}
return 0;
}
Correction to your code...
int guessed(char a)
{
int z;
char guess [6] = {0};
z = 0;
while(findletter(guess[z]) != 1 && findletter(guess[z]) < 6)
{
z++;
if(findletter(guess[z]) == 1)
return 1;
else
return 0;
} // you forgot closing while loop here
} // function closing parenthesis
//word guessed
Advice:
I don't know how much you had practice and how much you had learned yet..but on observing your mistake above I would like to suggest that whenever you create function or loop always write its prototype first, let's say you want to create a function for adding two numbers..
STEP 1: write prototype
int add(int x, int y)
{
//then do your stuff here...
return 0;
}
This will eliminate you chances of making error of parentheses...
There are a lot of issues with this program, from both a syntax standpoint and a logical one.
General issues include:
Function guessed and its while loop are not closed (missing }).
There is a lot of unused code (functions and variables).
The line if((findletter(secret[i] == 1))) compares the character value of secret[i] with 1 and passes that result to findletter. This doesn't matter though since you don't use this argument, and take user input within the function.
You have hardcoded strings and lengths, which makes your program less dynamic and harder to change in the future.
Using while loops as guards (in the unused functions tryguess and guessed), that are always exited on the first iteration.
findletter simply checks if secret contains the character c, returning on the first occurrence.
It could be more clearly expressed as:
int findletter(char unused) {
char secret[] = "secret",
c;
scanf(" %c", &c);
for (size_t i = 0; i < strlen(secret); i++)
if (secret[i] == c)
return 1;
return 0;
}
With that said, findletter would be better if you passed both the secret and c as arguments, so that you can use it more generically, and decouple user input from the function itself.
(Or you could simply use the standard library function strchr which achieves a very similar goal.)
The pattern of
if (a == b)
return 1;
else
return 0;
can simply be reduced to
return a == b;
Aside from the issues above, the structure of your program doesn't make much sense. If our program worked, you'd basically be asking the player to guess a word of unknown length, one character of the word at a time. They can also simply guess any letter to display the current one. One could 'solve' the entire word "secret" by simply inputting 's' repeatedly.
The structure of a very basic hangman program is:
Select the word to be guessed. Select number of lives.
Create a blanked version of word to track progress. Display this blanked version, which indicates the length to the player.
Ask the player to guess a letter. Skip those already guessed.
Update all positions in the blanked version where letter appears in the word.
Decrement lives on miss, end game if out of lives.
Check if the amount of characters changed in the blank version matches the length of word.
Win condition, or return to step 3.
There are many different ways to achieve this, and there are likely thousands of examples online.
Here is a rough program that is about as simple as it gets. This showcases the usual structure and flow of a game of hangman.
#include <stdio.h>
#include <string.h>
size_t update_all(char *to, const char *from, size_t len, char g) {
size_t changed = 0;
for (size_t i = 0; i < len; i++)
if (from[i] == g) {
to[i] = g;
changed++;
}
return changed;
}
void play_hangman(const char *word, unsigned lives) {
size_t word_length = strlen(word),
blanked_length = 0;
char blanked[word_length + 1],
guess = '\0';
for (size_t i = 0; i < word_length; i++)
blanked[i] = '*';
blanked[word_length] = '\0';
while (lives) {
printf("The word: [%s]\n"
"(Lives = %u) Enter a guess: ",
blanked,
lives);
scanf(" %c", &guess);
if (strchr(blanked, guess)) {
printf("[%c]: Already guessed!\n", guess);
continue;
}
size_t found = update_all(blanked, word, word_length, guess);
blanked_length += found;
if (!found) {
printf("[%c]: NOT FOUND!\n", guess);
lives--;
} else
printf("[%c]: FOUND!\n", guess);
if (!lives)
puts("Out of lives! You lose!");
else if (blanked_length == word_length) {
printf("You win! Word is [%s].\n", word);
return;
}
}
}
int main(void) {
play_hangman("secret", 10);
}
Note that this program is far from perfect, as it doesn't fully keep track of guessed letters, so the player can guess the same wrong letter multiple times, and lose a life every time. To fix this, we would need even more state, collecting each guess the player makes, and use that data instead of the naive if (strchr(blanked, guess)).
It also makes use of the '*' character as a sentinel value, which would cause confusion if our word contained '*'. To fix this, we could use an array of boolean values indicating the correctly guessed letters in the word thus far, and use this to print our word character-by-character. Or we could restrict character inputs with functions like isalpha.
This program simply serves as an example that for a proper approximation of the typical "Hangman" you need to handle more game state than you have.
(Error handling omitted for brevity throughout this answer.)

Trouble understanding Functions in C

I am working through a programming assignment. I had the key validation working within main but decided to try to make it a separate function. I do not understand functions very well yet so I am unable to see where I am going wrong. Whenever I run the program, I always just get "Key is Valid" even when I know it's not. As I said, the program was running fine in main.
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int validate (int c, string v[]); //prototpe validate function
int main (int argc, string argv[])
{
int validate (int argc, string argv[]); //run validate for argc and argv
printf("Key is valid\n"); //if valid, print message
}
int validate (int c, string v[])
{
//Validate that only one Command Line Argument was entered
if (c != 2) //Check the number if entered commands at execution
{
//If more than one, print error message
printf("Key must be the only Command Line Argument\n");
return 1; //Return false
}
//Validate that Key length is 26 characters
if (strlen(v[1]) != 26) //Don't forget null 0
{
printf("Key must contain exactly 26 characters\n");
return 1; //Return false
}
//Validate that all Key characters are alphabetic
for (int i = 0, n = strlen(v[1]); i < n; i++)
{
//Check each element of the array
if (isalpha(v[1][i]))
{
continue; //Continue if alphabetic
}
else
{
//if non-alphabetic, print error code
printf("Key must contain only alphabetic characters\n");
return 1; //Return false
}
}
//Validate that each Key character is unique
for (int x = 0, z = strlen(v[1]) - 1; x < z; x++)
{
//Create a second loop to compare every element to the first before incrementing the first
for (int y = x + 1; y < z; y++)
{
//Cast array elements to int, check if each element equals next element in array
if (v[1][x] == v[1][y])
{
printf("Key must contain exactly 26 unique characters\n");
return 1;
}
}
}
return 0; //Key is valid, so return true
}
You are just declaring the function validate instead of running that and printing Key is valid unconditionally.
To run the function validate and print Key is valid only if it returns 0, the main function should be like this:
int main (int argc, string argv[])
{
if (validate (argc, argv) == 0) //run validate for argc and argv and check its response
{
// put printing inside if statement so that it runs only if the condition is true
printf("Key is valid\n"); //if valid, print message
}
}
Function declaration hints the compiler that I will return a particular data type and I will accept the given data types in the arguments section. For example,
int check(int a, int b); -> return type is int and the function will accept 2 integer parameters.
int mul(int a, float b); -> return type is int and the function will accept 1 integer parameter and 1 float.
void check(); -> returns nothing, accepts nothing
function calling is like your code is calling the function to execute.
int c = check(2, 3);
int b = mul(3, 0.12);
check();
Your function is returning some value. You have to get that value and execute rest of the code based on the value as below.
if (validate (argc, argv) == 0) {
printf("key is valid");
} else {
printf("key is not valid");
}

Error in c when iterating: use of undeclared identifier i

I am creating a program that will ask a command line argument from the user, and the user need to input only integers as the argv[1]. It should reject any input other than integers. My code is as below:
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, string argv[] )
string s = argv[1];
for (int i = 0; n = strlen(s); i< n; i++)
{
if(!( isdigit(s[i])) )
{
printf("All numbers: correct input");
return 1;
}
}
//Else print a prompt asking for a plaintext to cipher
else
{
string p = get_string("Your text here: ");
return 0;
}
}
Running the above code throws me an error: error: use of undeclared identifier 'i' for (int i = 0; n = strlen(s); i< n; i++)
Where do I do wrong here and how do I fix this? Thanks.
Sorry if my questions seem stupid, I am still a newbie learning here and know absolutely nothing about C before. Thanks for the help though.
If you have a C89 compiler you will need to put the delarations at the top of the scope block, so the variable i has to be declared before the for loop.
Try this if your using C89:
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, string argv[] ){
string s = argv[1];
int i = 0;
int n = strlen(s);
for (; i< n; i++)
{
if(!( isdigit(s[i])) )
{
printf("All numbers: correct input");
return 1;
}
}
//Else print a prompt asking for a plaintext to cipher
else
{
string p = get_string("Your text here: ");
return 0;
}
} //end of for loop? TODO: fix your braces spacing the sample was broken
}//end of for main? TODO: fix your braces spacing the sample was broken

Why is my password checking code not working correctly? It gives same output regardless of whatsoever input

I made a password checking program which checks for the following criteria:-
Should have atleast
1 uppercase
1 lower case
1 special character
1 number
Should be lesser than 100 characters
and thats it. I have not given any lower limit. And no matter what input I give (correct or incorrect), the program gives me the same or similar output as attached in my screenshot.
For eg:- Pratik10, pratik10, pratikten, pr#tiK10, I get the same output "Password is fine and valid".
Why is my program not checking the defined conditions correctly? It is not even printing the counters of the password correctly.
Following is my code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
int main()
{
char x[100];
int i;
int uc=0;
int lc=0;
int num=0;
int misc=0;
printf("enter your password\n");
scanf("%s",x);
for(i=0;i<100;i++) {
if (isalpha(x[i])) {
if (isupper(x[i])) {
uc++;
}
if (islower(x[i])) {
lc++;
}
}
if (isdigit(x[i])) {
num++;
}
else {
misc++;
}
}
printf("checking your password\n");
printf("%d uc\n",uc);
printf("%d lc\n",lc);
printf("%d num\n",num);
printf("%d misc\n",misc);
if ((uc > 0) && (lc > 0) && (num > 0) && (misc > 0)) {
printf("password is fine and valid\n");
}
else {
if(lc<=0) {
printf("lowercase character(s) missing cannot proceed without inclusion\n");
}
if(uc<=0) {
printf("uppercase character(s) missing cannot proceed without inclusion\n");
}
if(num<=0) {
printf("number(s) missing cannot proceed without inclusion\n");
}
if(misc<=0) {
printf("special character(s) missing cannot proceed without inclusion\n");
}
printf("please include all the missing parameters in combination to validate the password and try again\n\n");
}
return 0;
}
How to correct this?
Output:
The problem is that you are checking the whole array, which is mostly uninitialized and contains all kind of characters at random.
Therefore, you have to exit the loop when you encounter a '\0' character.
You should check only the null-terminated string provided as input by the user.
In other words, you should iterate x until you encounter the null-character.
Change this:
for (i = 0; i < 100; i++)
To this:
for (i = 0; x[i] != 0; i++)
A second problem is that you are not using if/else properly.
As a result, every character which is not a digit is counted as misc.
Change this:
if (isdigit(x[i]))
To this:
else if (isdigit(x[i]))
Other answers have mentioned the main problem. There is still one more problem: There is a missing else before if (isdigit(x[i])) {num++;},
for(i=0; x[i]!=0; i++)
{
if (isalpha(x[i]))
{
if (isupper(x[i])) {uc++;}
if (islower(x[i])) {lc++;}
}
else if (isdigit(x[i])) {num++;} // a missing else
else {misc++;}
}

Resources