counting characters (1 Viewer)

untouchablecuz

Active Member
Joined
Mar 25, 2008
Messages
1,693
Gender
Male
HSC
2009
hey guys. i'm not sure how to solve this and if anyone could give me a leg up it would awesome (i don't actually WANT the code for the task, just a tip or something)

what i want to do is identify the number of characters that are inputted into scanf(). what i CAN do is identify the number of numbers etc that are inputted (using x=x+1 type of thing), but that doesnt help me

i.e. i want this:

i want the user to input 17 characters into the program

output: system prints you have inputted 17 characters

in terms of code:

scanf("something something", &something)

if (the number of characters inputted equals to 17) {
printf("17 characters inputted");
} else if (the number of characters inputted equals to 18) {
printf("18 characters inputted");
} else {
printf("Not 18 or 17 characters inputted);
}

in general, how do i count the number of characters inputted?

e.g. system Please enter a random number:

user: 456789

system: This number has 7 characters

(edit: sorry if im abit jumbled up)
 

LordPc

Active Member
Joined
May 17, 2007
Messages
1,370
Location
Western Sydney
Gender
Male
HSC
2008
the answer is really simple and its got nothing to do with program flow or logic, just the syntax of how to print a variable to the screen which i would bet you will learn in the next lecture

anyway, heres the explanation. i assume you are familiar with scanf already.

Code:
scanf("%d\n", &exampleVariableName);
the %d incidates that scanf is looking for an integer (d is for decimal. printf works in a very similar way

Code:
printf("you entered %d\n", exampleVariableName);
printf will replace the %d with the value of exampleVariableName. note that scanf and printf are very similar but not identical, there is no & in the printf statement

i trust that you can now see how you can replace multiple if statements with a single printf statement
 

untouchablecuz

Active Member
Joined
Mar 25, 2008
Messages
1,693
Gender
Male
HSC
2009
hmm, i already know all that. i don't think you get me

what i mean is:

for example,

Code:
scanf("%d\n", &num)

if ([B]num is composed of 7 characters[/B]){
       x=x+1;
} else if ([B]num is composed of 8 characters[/B]) {
       x=2x;
} else {
       x=x%2;
}
understand what i'm getting at? i just used printf functions as an example, anything could replace them. i just need a way of translating the guards into code so that the system can make a choice depending upon the user input

thanks LordPc
 
Last edited:

acmilan

I'll stab ya
Joined
May 24, 2004
Messages
3,989
Location
Jumanji
Gender
Male
HSC
N/A
Theres a few ways to do it if im understanding your problem. For example:

I'm assuming by number you mean integer, so:
A number has 7 characters if it is > 999999 and < 10000000
A number has 8 characters if its > 9999999 and < 100000000
etc

It's probably not the nicest way to do it but it should work unless im reading your problem wrong.

Another thing you could look at is the getchar() function, strlen() etc.

You could convert the input to a number and constantly divide by 10 until the answer is <1. However many times youve divided by 10 is how many characters the number has. Eg 16346 divided by 10 becomes 1634.6, then 163.46, then 16.345, then 1.6345, then 0.16345. So it took 5 goes to get it <1, so it has 5 characters.
 
Last edited:

LordPc

Active Member
Joined
May 17, 2007
Messages
1,370
Location
Western Sydney
Gender
Male
HSC
2008
Theres a few ways to do it if im understanding your problem. For example:

I'm assuming by number you mean integer, so:
A number has 7 characters if it is > 999999 and < 10000000
A number has 8 characters if its > 9999999 and < 100000000
etc

It's probably not the nicest way to do it but it should work unless im reading your problem wrong.

Another thing you could look at is the getchar() function, strlen() etc.

You could convert the input to a number and constantly divide by 10 until the answer is <1. However many times youve divided by 10 is how many characters the number has. Eg 16346 divided by 10 becomes 1634.6, then 163.46, then 16.345, then 1.6345, then 0.16345. So it took 5 goes to get it <1, so it has 5 characters.
ok, i dont understand the problem that OP. is it with the syntax of using an if statement? like

Code:
scanf("%d\n", &num)

if (num > 7){
       //do something
} else if (num > 8) {
       //do something
} else {
       //do something
}
or do you want some help in creating a way of counting characters in c? like if they enter any number of characters, you want to tell them how many they entered? (type wc -m into a unix terminal, hit enter, type some letters and hit enter again. you will get a number. is this what you want to be able to do?)

otherwise i hope acmilan answered your question cause im stumped
 

untouchablecuz

Active Member
Joined
Mar 25, 2008
Messages
1,693
Gender
Male
HSC
2009
acmilan answered it perfectly :) funnily enough, i thought of that exact same solution in my comp tute today

thanks for the help fellas :eek:
 

youngmin

New Member
Joined
Mar 18, 2010
Messages
24
Gender
Male
HSC
2009
If this is for the COMP1917 assignment, scanf will fail at stage 5.
Obviously that's not a problem if you're COMP1911.
 

untouchablecuz

Active Member
Joined
Mar 25, 2008
Messages
1,693
Gender
Male
HSC
2009
If this is for the COMP1917 assignment, scanf will fail at stage 5.
Obviously that's not a problem if you're COMP1911.
nah COMP1911

and yer i figured, but how else can you input numbers without using scanf? :S
 

youngmin

New Member
Joined
Mar 18, 2010
Messages
24
Gender
Male
HSC
2009
There's this thing called getchar and putchar, we're learning it now. It's more complicated than scanf but yeah, can't do stage 5 without it :(
 

youngmin

New Member
Joined
Mar 18, 2010
Messages
24
Gender
Male
HSC
2009
Ahaha yeah, COMP1917 is my favourite subject.
I've barely studied my other ones :)

You?
 

Prosper

Member
Joined
Jan 29, 2008
Messages
171
Gender
Male
HSC
2008
Just wait 'til you do strings, you'll never want to use putchar/getchar again :p
 

appletoa

Member
Joined
Oct 10, 2009
Messages
80
Gender
Male
HSC
2009
Just wait 'til you do strings, you'll never want to use putchar/getchar again :p
Yeah, that's pretty much what my tutor said. Suppose there could be a bit of an advantage in knowing how to use the basic commands though, in that you get a little more insight into how to use C. Helpful for people who've never programmed before, or people (like me) who haven't programmed in C before.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top