Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm being bugged right now because I'm making a little password validation thing in C.
the case is
It should have at least 8 characters entered
It should have at least 1 number
It should have at least 1 uppercase / lowercase letter.
For example:
if the rules are not complied it would show
Enter your password: doratheexplorer
NOT A GOOD PASSWORD
NO DIGIT
NO UPPERCASE LETTER
else
continue
how would I do something like this? do I need to use string.h?
Suppose your password is stored in a character array named password. Then you can do the validation as follows including string.h:
int digitflag=0;
int upperflag=0;
int lowerflag=0;
int i;
if(strlen(password)<8)
{
printf("Length is less than 8");
}
else
{
for(i=0;i<strlen(password);i++)
{
if(isdigit(password[i]))
digitflag=1;
if(isupper(password[i]))
upperflag=1;
if(islower(password[i]))
lowerflag=1;
}
if(digitflag==0)
printf("No digits");
if(upperflag==0)
printf("No Upper Case Letters");
if(lowerflag==0)
printf("No Lower Case Letters");
}
At first you should check length of the password if it is less than 8 then you can give a error message.And for digit,uppercase and lowercase character you can use separate flag and then you have to read each character set appropriate flag.When you finished reading all character you can check whether all flags are set or not and take appropriate actions.
Related
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 3 years ago.
Improve this question
I was making a script that is calculating the additions between two natural numbers which decimal lengths should be smaller or same with 10000, and printing a result of the sum.
Of course, there ain't any variable type that can hold a integer which length is 10000 in C.
So, I made the program by utilizing the simple additions' calculating logic that all we learn in a school when we were young. And also, I just should use strings to get those gigantic numbers.
But some results were starting with zero. I knew why did the zero appeared there, but I did prefer to have a result that is like "1234", not "01234". By the way, all other stuffs were perfect.
I needed a function that gets input as string, and erases a single zero starts with a string if it exists.
And could you make it instead of me, please? You should probably consider that the strings we will deal with can have such a length that is smaller or same with 10000.
Maybe this:
char * f( char * str )
{
while ( *str == '0' && str[1] )
str++; // skips all zero-s when it is not last character in string
return str;
}
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 6 years ago.
Improve this question
I have this code that will print numbers from 0 to 9 in english words (like one for 1, two for 2, etc.). What if I wanted to print 374? Or something much larger, like 7549846451?
#include <stdio.h>
int main()
{
double sum;
if(scanf("%1f",&num)!=0)
{
if(num=(int)num)
{
switch((int)sum)
{
case 0:printf("zero\n");break;
case 1:printf("one\n");break;
case 2:printf("two\n");break;
case 3:printf("three\n");break;
case 4:printf("four\n");break;
case 5:printf("five\n");break;
case 6:printf("six\n");break;
case 7:printf("seven\n");break;
case 8:printf("eight\n");break;
case 9:printf("nine\n");break;
default:printf("not a digit"); break;
}
}else
{
printf("Invalid")
return 0;
}
}
return 0;
}
This is a good start, but it would take a lot more to complete your program:
Start by expanding your code to printing numbers 10..99. There would be a special case for 11..19, but after that it's pretty regular. The lower 20 can be addressed with a lookup table. In fact, making a look-up table for the whole range wouldn't be too bad, either.
With a routine that writes out numbers 0..99 in hand you can expand into hundreds by looking at the third digit the right, writing it out, adding "hundred", and proceeding to writing out the number 0..99
Now that you have a routine for writing out three-digit numbers all you need is to split your number into groups of tree, calling this routine for non-zero groups, and adding "billion", "million", and "thousand" corresponding to the rank of the group.
Here you have the solution to your problem. It is even the same example as you have pasted here, so if you have read the comments below, you'd have seen the comment form Bheema in which he posted the whole code for it.
Also, you can try writing your own code, it's not that hard. dasblinkenlight gave you instructions how to do it.
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 8 years ago.
Improve this question
I am comparing two strings each coming from arrays.
while(countX<10){
if(strcmp(scanWord[countX], currentWord[countX]) == 0)
{scoreCurrent++;scoreCurrent++;}
countX++;
}
"scanWord[countX]" and "currentWord[countX]" don't compare; each time coming up that they aren't the same even if they are. It works if I compare things that aren't those and I have printed them to screen to check too. They just don't seem to play well. Thanks in advance.
When you're reading the line, remove the newline:
char *line = fgets(currentWord[countX], 20, stdin);
if (line) {
int len = strlen(line);
if (line[len-1] == '\n') {
line[len-1] = 0;
}
}
The question as I see it:
"scanWord[countX]" and "currentWord[countX]" don't compare; each time coming up that they aren't the same even if they are.
How do you KNOW the two values are the same? Print them out. Print out the length too. If dealing with Unicode of any sort, print out the individual bytes in hexadecimal, because two Unicode characters that look identical may actually be different.
Because the computer will never say the strings are different if they are really the same. The answer to the question as I read it is that the strings actually are different.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I am trying to get an input from a text file,whose name is of the format
(without spaces) .Eg, 3shop where 3 is number of inputs in file.
How do I extract this number from the string ?
Also content of the file is of same format and variable length.
**i.e complete 3shop.txt is of the form
1soap 3toothpate 5biscuits
8biscuits
9toothpaste 5 soap
There is no limit on size of integer,otherwise i could have used an array and extracted the number.
Please, suggest some good ways to do this.
Thanks in advance.
char name[32] = "3shop.txt", *p = name;
long id;
id = strtol(name, &p, 10);
printf("id=%ld name=%s\n", id, p);
10 in strtol means base 10 (decimal)
After the call to strtol, p points to the next character in name after the numerical value
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
#include <stdio.h>
int hledejznak(x)
{
int c;
int pocitadlo=0;
while((c=getchar())!=EOF)
{
if(x==c){
pocitadlo++;
while((c=getchar())!=32)
{
printf("%d\n",c);
};
};
};
return pocitadlo;
}
int main(int argc,char *argv[])
{
int znak=*argv[1];
printf("answer is %d",hledejznak(znak));
return 0;
}
Hi people, I need to count words containing character specified as argument at terminal
example: echo 'hello babe' | ./main e
Answer is 2
....because there are two words containing letter "e"
My code doesn't work, can you help me?
Thanks
Don't nest your loops; keep the outer one that processes each character read
Have a boolean variable initalized to false & set to true whenever you see the desired character.
Whenever a word ends, increment your counter if the flag is true. Either way, set the flag to false (to get ready for the next word). (Note that the last word may NOT end with a space.)
Only when you're processed all of the input should you print the value of the counter.