Reputation: 2315
I was having some problem when trying to loop thru a string starting from right hand side based on the digit position entered by user. So basically this will be the output of my program:
Enter a number: 1234567
Enter the digit position: 3
digitValue1(): 5
And here is my code:
int main()
{
int num, digit, result;
printf("Enter the number: ");
scanf("%d", &num);
printf("Enter the digit position: ");
scanf("%d", &digit);
printf("digitValue1(): %d\n", digitValue1(num, digit));
return 0;
}
int digitValue1(int num, int k)
{
int result, i;
char str[15];
sprintf(str, "%d", num);
int len = strlen(str);
for (i = 0; i < len; i++) {
printf("%d ", i);
if (i == k) {
result = k;
}
}
return result;
}
So from what I did in the digitValue1()
is I take the int num entered by user and converted it into string and loop thru. However, it started from left hand side. So this is my output:
Enter a number: 1234567
Enter the digit position: 3
digitValue1(): 3
I was thinking how should I make the loop start from right hand side. I tried to reverse the for loop and it doesn't make sense.
Thanks in advance.
Upvotes: 1
Views: 2569
Reputation: 724
As you are starting from 0 position. If you want to print from right then start from right. For that we need to set initialize value as len and ending with 0.
for (i = len-1; i >= 0; i--)
{
}
Upvotes: 0
Reputation: 870
You can change your for loop from your side as well like following
for (i = len-1; i >= 0; i--)
{
// your implementation code
}
Upvotes: 0
Reputation: 45720
Initialize i
to start at the end. Remember that strlen()
is a Natural number, so the index of the last character is 1 less.
There are a few other details to note:
This does not actually reverse the string being scanned; rather it starts scanning from the end (right-side) of the string.
Since the loop is starting at the end of the string and working towards the front, the stopping condition needs to adjust to look for the k
th-last character.
We want the integer value of the result digit to be returned, so we must convert the char
to an int
. That's easiest done by subtracting the ascii value for '0' from the result digit.
int digitValue1(int num, int k)
{
int result, i;
char str[15];
sprintf(str, "%d", num);
int len = strlen(str);
for (i = len-1; i >= 0; i--) { // Loop backwards from end of str
printf("%d ", i);
if (len-i == k) { // Check if we are k from end
result = str[i] - '0'; // Convert digit char to int
}
}
return result;
}
This remains an exhaustive search... it will continue through the whole string even after finding the k
th-last element. (Just as your original question did.) You'll want to think about how to optimize that, so it stops looking once the result is identified.
Instead of a loop though, it would be more effective to calculate the index you're looking for:
result = str[strlen(str)-k] - '0';
Upvotes: 2
Reputation: 8576
Well, I may note a few things in your code to let you avoid future bugs:
sprintf()
instead of snprintf()
. This is (almost) as dangerous as using the evil gets()
.strlen()
returns a size_t
, not an int
(this can cause really serious issues)!And finally, you appear to not understand the for
loop. Just remember this: in C(-like) languages, a for
loop is not exactly an iterator over a pseudo-list. In these languages, this:
for(initializer; condition; helper) {
...
}
Is completely equivalent and shorthand for:
{
initializer;
while(condition) {
...
helper;
}
}
Thus, taking all this stuff into account, digitValue1()
could be reworded as something like this (note that the first position
is zero):
int digitValue1(int num, int position) {
do {
int digit = num % 10;
num /= 10;
if(position-- == 0)
return digit;
} while(num != 0);
// This shall be understood as an error
return -1;
}
Hope this helps! Cheers, KemyLand.
Upvotes: 0