I'm trying to create a c program that checks if the password contains at least :
-1 special character
-1 lowercase letter
-1 uppercase letter
-1 digit
when i test it, it doesn't work for some reason i've been trying to figure out my error but i didn't know what was wrong
this is my attemp :
//check password =at least one upper /at least 1 lower/ at least 1 char;
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
void main() {
char pass[20]; //password
int a = 0; //size of the password
int foundChar = 0;
int foundUpper = 0;
int foundLower = 0;
int foundDigit = 0;
int i = 0;
do {
printf("enter your password : ");
scanf("%s", pass);
a = strlen(pass);
if (a = 8) {
if (isdigit(pass[i]) && foundDigit == 0) {
foundDigit = 1;
i++;
} else if (isupper(pass[i]) && foundUpper == 0) {
foundUpper = 1;
i++;
} else if (islower(pass[i]) && foundLower == 0) {
foundLower = 1;
i++;
} else if (foundChar == 0) {
foundChar = 1;
i++;
}
}
}
while ((a < 8) || (foundChar == 0) || (foundDigit == 0) || (foundUpper == 0) || (foundUpper = 0));
}
You are not looping through the password.
After your if (a >= 8) statement, there is no code to loop around and examine the remaining characters in the string.
Related
I am try to implement a program to do this type of calculation in which i have to use only some limited system calls
fork
exec* family
str* family
ato* family
printf, sprintf
round
The program name is double.c and the calculation looks like
./double square 3
output:36 as square(double(3))
I am using exit(result) to returning the result but as the range it is capable of is 0-255 but when the number exeeds from 255 it gives wrong result I have to impement this program just using these system api calls so i think pipe and munmap cannnot be used but how i can return bigger values than using just these functions
What i tried is
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <math.h>
#include <sys/wait.h>
#include <stdlib.h>
#define EPSILON 0.001
double simple_abs(double x)
{
return x > 0 ? x : -x;
}
double simple_sqrt(double x)
{
double previous = 0;
double guess = x;
while (simple_abs(guess - previous) > EPSILON)
{
previous = guess;
guess = previous - (previous * previous - x) / (2 * previous);
}
return guess;
}
int lop(int status) {
return (status >> 8) & 0xff;
}
int square(int n) {
printf("number = %d and square = %d\n", n , n*n);
return n * n;
}
int root(int n) {
return (int) simple_sqrt((double) n);
}
int doubleVal(int n) {
return n * 2;
}
int main(int argc, char *argv[]) {
int value = atoi(argv[argc - 1]);
int result = value;
if (argc > 2){
result = doubleVal(result);
for (int i = 1; i < argc - 1; i++) {
int pid = fork();
if (pid == 0) {
if (*argv[i] == 's' && *(argv[i]+1) == 'q' && *(argv[i]+2) == 'u' && *(argv[i]+3) == 'a' && *(argv[i]+4) == 'r' && *(argv[i]+5) == 'e' && *(argv[i]+6) == '\0'){
result = square(result);
} else if (argv[i][0] == 'r' && argv[i][1] == 'o' && argv[i][2] == 'o' && argv[i][3] == 't' && argv[i][4] == '\0') {
result = root(result);
} else if (argv[i][0] == 'd' && argv[i][1] == 'o' && argv[i][2] == 'u' && argv[i][3] == 'b' && argv[i][4] == 'l' && argv[i][5] == 'e' && argv[i][6] == '\0') {
result = doubleVal(result);
} else {
printf("Unknown operation: %s\n", argv[i]);
exit(1);
}
printf("Child result: %d\n", result);
//return result;
exit(result);
} else {
int status;
wait(&status);
//result = (status >> 8) & 0xff;
result = lop(status);#lop is working as WEXITSTATUS system api
}
}
}
else {
result = doubleVal(result);
}
printf("Result: %d\n", result);
return result;
}
Is there any way to do this to get correct result because when i try ./double square 8 it give wrong result as the result exceeds from 255 .
Is there more elegant way to do this task?
Program asks user for integer and repeats if non-digital characters are entered.
To exit loop two conditions expected:
a) all entered characters are digits
b) last character is '\n'
Short solutions like scanf don’t work properly, other approaches require lots of variables loops and if else conditions. User input is common task and I would like to have proper reusable template.
Subjective opinions are appreciated. Way to simplify this function or advice on another solution. Improve formatting. Reading for more systematic understanding.
#include <stdio.h>
int getIntOnly();
int main() {
int x = 0;
x = getIntOnly();
printf("\nvalue entered is: %d \n", x);
}
int getIntOnly() {
int ch, num, quit, abc;
do {
num = 0;
ch = 0;
quit = 0;
abc = 0;
printf("Enter the input: ");
do {
ch = getchar();
if (ch >= 48 && ch <= 57) {
num = num * 10 + (ch - 48);
}
else if (ch == '\n') {
quit = 1;
}
else {
abc = 1;
}
}
while (quit == 0);
}
while (quit == 0 || abc == 1);
return (num);
}
Using fgets() means you'll get the full text at once.
You can then examine it (and convert it too) to suit your needs.
int getIntOnly( void ) {
int value = 0, i = 0;
char buf[ 64 ];
do {
printf( "Enter integer value: " );
fgets( buf, sizeof( buf ), stdin );
value = 0;
for( i = 0; '0' <= buf[i] && buf[i] <= '9'; i++ )
value = value * 10 + buf[i] - '0';
} while( buf[i] != '\n' ); // good! reached end of line
return value;
}
May be better? Add some validity checks for the result of fgets() and strtol() according to your original code.
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
int getIntOnly();
int main() {
int x = 0;
x = getIntOnly();
printf("\nvalue entered is: %d \n", x);
}
bool isDigit(char ch) {
return (ch >= '0' && ch <= '9')? true : false;
}
bool isAllDigit(char *buf) {
int i;
for (i = 0; buf[i] != '\n'; i++) {
if (isDigit(buf[i]) == false) {
return false;
}
}
return true;
}
bool isVaildInt(long int number) {
return (number >= INT_MIN && number <= INT_MAX)? true : false;
}
int getIntOnly() {
char buf[100];
long int num;
bool done = false;
do {
/* read line-by-line */
fgets(buf, 100, stdin);
if (isAllDigit(buf) == false)
continue;
num = strtol(buf, NULL, 10);
/* strtol() returns long int */
if (isVaildInt(num) == false)
continue;
done = true;
} while (done == false);
return num;
}
I have to examine passwords. The password is strong, when it contains uppercases, lowercases, digits and at least 8 characters. I wrote this C program, but it always displays "weak password". I don't know the reason.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define N 50
int strenght(char word[],int length)
{
int sup = 0;
int low = 0;
int dig = 0;
for(int i = 0; i < length; i++)
{
if(isupper(word[i]) == 1)
sup++;
else if(islower(word[i]) == 1)
low++;
else if(isdigit(word[i]) == 1)
dig++;
}
if(sup > 0 && low > 0 && dig > 0 && length >= 8)
return 1;
else
return 0;
}
int main()
{
printf("Type the password until '*'\n");
char word[N];
while(1)
{
printf("Password: ");
fgets(word, N, stdin);
int length = strlen(word) - 1;
if(word[0] == '*')
break;
else
{
if(strenght(word, length) == 1)
printf("Strong password\n");
if(strenght(word, length) == 0)
printf("Weak password\n");
}
}
return 0;
}
The problem is that you are comparing the results of the isupper, islower and isdigit calls to 1. Don't do this! Each of these functions will return zero if the condition is not satisfied and any non-zero value if it is satisfied. (see cppreference).
So, in place of:
if(isupper(word[i]) == 1)
sup++;
just do this:
if(isupper(word[i]))
sup++;
or, if you want to keep the explicit nature of the comparison, use:
if(isupper(word[i]) != 0)
sup++;
(And similarly for the other tests.)
#include<stdio.h>
#include <stdlib.h>
int main()
{
long long int a;
int flag = 0;
scanf("%lld", &a);
if (a > 0)
{
while (a > 0)
{
if (a % 2 == 0 || a == 1)
{
a = a / 2;
flag = 1;
}
else
{
flag = 0;
break;
}
}
}
if (a < 0)
{
while (a <= -1)
{
if (a % 2 == 0 || a == -1)
{
a = a / 2;
flag = 1;
}
else
{
flag = 0;
break;
}
}
}
if (flag == 1)
{
printf("yes");
}
else
{
printf("no");
}
}
click this for output image
Given an integer N, the program must determine if it is a power of 2 or -2. If N is a power of 2 or -2, the program must print yes. Else the program must print no.
Boundary Condition(s):
-10^17 <= N <= 10^17
Input Format:
The first line contains the value of N.
Output Format:
The first line contains either yes or no
For the input -4503599627370496 it should print no but it prints yes. Solution, please
In case of negative numbers, you should also count which power of 2 is it. If it is even it won't work. -2 to the power 52 gives 4503599627370496 instead of -4503599627370496.
The code below solves this puzzle using bitwise shift operator:
#include <stdio.h>
#include <stdlib.h>
int main()
{
long long int a, aPositive, l = 1;
scanf("%lld", &a);
aPositive = a > 0 ? a : -a;
int count = 0;
while (l < aPositive)
{
l = l << 1;
count++;
//printf("%d\t%lld\n", count, l);
}
if (l == aPositive && (a > 0 || count % 2 == 1))
{
printf("yes");
}
else
{
printf("no");
}
return 0;
}
Very similar problem has been solved here: how-to-check-if-a-number-is-a-power-of-2
bool IsPowerOfTwo(long long x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}
int main()
{
bool result = IsPowerOfTwo(256);
if (result )
{
printf("yes");
}
else
{
printf("no");
}
return 0;
}
I am new at Processes. I read a lot but I didn't really understand how it works. I try to make a process for each vowel in a char string. I have to delete all vowels from that string. I know that I have to use fork, but I don't know how. I tried to write the code, but what I receive was Core Dumped.
#include <unistd.h>
#include <stdio.h>
#include <string.h>
char sir[100];
int vocal(char x)
{
if(x=='a' || x=='e' || x=='i' || x=='o' || x=='u' || x=='A'||
x=='E' || x=='I' || x=='O' || x=='U')
return 1;
return 0;
}
int main(){
printf("Read the text: \n");
read(1,sir,100); // file descriptor is 1;
pid_t a_Process;
for(int i=0;i<strlen(sir);i++)
{
if(vocal(sir[i])==1)
{
a_Process=fork();
for(int j=i;j<strlen(sir)-1;i++)
sir[j]=sir[j+1];
}
}
printf("%s",sir);
return 0;
}
I didn't understand how the child process and everything goes. Thank you very much!
Try this code:
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char sir[100];
int vocal(char x)
{
if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' ||
x == 'A' || x == 'E' || x == 'I' || x == 'O' || x == 'U')
return 1;
return 0;
}
int main()
{
int i, j, pid_status;
printf("Read the text: \n");
// read(1,sir,100); // file descriptor is 1;
fgets(sir, 100, stdin);
pid_t a_Process;
for (i = 0; i < strlen(sir); i++)
{
if (vocal(sir[i]) == 1)
{
printf("detected a vowel\n");
a_Process = fork();
if (a_Process == -1)
{
fprintf(stderr, "Can't fork a process.\n");
return 1;
}
if (a_Process)
{
printf("Starting a new child .... \n");
for (j = i; j < strlen(sir) - 1; j++)
sir[j] = sir[j + 1];
}
// The following statement is needed such that
// child process starts one after the other.
if (waitpid(a_Process, &pid_status, 0) == -1)
{
printf("Error waiting for child process.\n");
}
}
}
printf("%s", sir);
return 0;
}