printf width modifier for special characters [duplicate] - c

This question already has answers here:
Number of character cells used by string
(6 answers)
Closed 5 years ago.
i have a problem with printf width modifier in C
example:
char a[] = "o", b[] = "l";
printf("%-3s %s", a, b);
console output gives me 3 spaces between strings, but when I change "o" to "ó", console shows 2 spaces between. Every time i use character like "ł", "ó", "ś" in the string, width modifier is shortened by 1 sign, why this happens?
OS X 10.11 (El Captain)

"Special" characters as you are showing them need more bytes (char) to be represented in a string. Your arbitrary limit of 3 is not enough, raise it to some suitable value.
In addition, the way and length that such special characters are represented depends on the system. For portable code you should never make such arbitrary assumptions.

Related

Can you print format align with a variable in C? [duplicate]

This question already has answers here:
Set variable text column width in printf
(2 answers)
Closed 2 years ago.
// If I have an int, eg
int num=3;
//then how can I do this
printf(" %nums", some_string);
// to get it right aligned by 3 characters
context: I need to use a loop to print statements with variable alignments depending on the order they are printed, if I cant use a variable I cant do that.
Yes, it's in the printf manpage:
Instead of a decimal digit string one may write "*" or "*m$" (for some
decimal integer m) to specify that the field width is given in the
next argument, or in the m-th argument, respectively, which must be of
type int.

why gets() not works and scanf() works here [duplicate]

This question already has answers here:
How to read / parse input in C? The FAQ
(1 answer)
What's the difference between gets and scanf?
(10 answers)
Why is the gets function so dangerous that it should not be used?
(13 answers)
scanf() leaves the newline character in the buffer
(7 answers)
Closed 4 years ago.
I was solving the question described below. Link of question is https://www.codechef.com/problems/CHEFSTLT
Chef has found two very old sheets of paper, each of which originally contained a string of lowercase Latin letters. The strings on both the sheets have equal lengths. However, since the sheets are very old, some letters have become unreadable.
Chef would like to estimate the difference between these strings. Let's assume that the first string is named S1, and the second S2. The unreadable symbols are specified with the question mark symbol '?'. The difference between the strings equals to the number of positions i, such that S1i is not equal to S2i, where S1i and S2i denote the symbol at the i the position in S1 and S2, respectively.
Chef would like to know the minimal and the maximal difference between the two strings, if he changes all unreadable symbols to lowercase Latin letters. Now that you're fully aware of Chef's programming expertise, you might have guessed that he needs you help solving this problem as well. Go on, help him!
Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of a test case contains a string S1.
The second line of a test case contains a string S2.
Both strings consist of lowercase Latin letters and question marks in places where the symbols are unreadable.
Output
For each test case, output the minimal and the maximal difference between two given strings separated with a single space.
and my code was
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void){
int t;
scanf("%d",&t);
while(t--){
fflush(stdin);
char a[100],b[100];
gets(a);gets(b);
int l = strlen(a);
int min=0,max=0,i=0;
while(i<l){
if(a[i]=='?' || b[i]=='?'){
max++;
}
else if(a[i]!=b[i]){
min++;
max++;
}
i++;
}
printf("%d %d\n",min,max);
}
return 0;
}
but this gives me the wrong answer until I take input using scanf() instead of gets(). My question is why this happens.
Please note this "gets() considers a whitespace as a part of the input string and ends the input upon encountering newline or EOF."
So there might be input as "something otherThing"
For more See this

Variable for Altering String Output in `printf()` - C Language [duplicate]

This question already has answers here:
Set variable text column width in printf
(2 answers)
Closed 6 years ago.
In C, as I understand so far, this would be correct:
printf("%10s\n", "This is C");
would return:
" This is C!"
(with the intentional space prior to the string; no quotations).
My question is can you replace the 10 specifying the length of the print using a variable? If so, how?
That's how:
printf("%*s\n", 10, "This is C");
The format changed from %10s to %*s. The printf() now would expect among the argument, before the string, an int with the width to pad the string to (the 10 in the example above; obviously could be a variable too).
To tell the printf() to pad the output to the left (instead of the default right) use the -: %-*s. (The output would change from " This is C" to "This is C ".)
To tell the printf() to take only few first bytes from the string, or if the string is not null-terminated, you can add .* to the format at the same place as the precision for floating point types. The printf() would print up to that number of characters, stopping at the first null character. Example:
int width = 10;
int chars = 4;
printf( "%-*.*s", width, chars, "This is C" );
would produce output "This ".

Print from one position to another - using printf [duplicate]

This question already has answers here:
Is it possible to print out only a certain section of a C-string, without making a separate substring?
(3 answers)
Closed 7 years ago.
I want to do the following, taking into account a string as how are you I want to print from a until e ie the word are. I tried this
char str[] = "how are you";
printf("%*.*s\n", 5, 8, str);
Output
how are
Expected output
are
Someone has idea how this can be done?
The characters in the string are indexed from 0, so the a is at index 4. By passing &str[4] to printf, the printf will start at the a, and print as many characters as specified by the precision.
printf("%.*s\n", 3, &str[4]);
You are specifying field width and number of chars to print. However, the string always starts from the pointer as passed, there is no "start offset" specifier, as that would be unnecessary. See here for the format specifiers.
For your code, that would be printf("%.*s", width, str + start)
Note that &str[start] is identical.
try using this code
printf("%.*s\n", 3, &str[4]);

How do I get a substring from a string in C? [duplicate]

This question already has answers here:
Copying a part of a string (substring) in C
(13 answers)
Get a substring of a char* [duplicate]
(5 answers)
Closed 8 years ago.
An MD5 encrypted password is $1$12345678$blahblahblah. The salt is the 8 digit key between the two $ signs after the one. How do i extract those 8?
So far I have char *salt = and i need to make it equal to the third-tenth index of the string.
Your question contains the word "extract" which is somewhat complicated because it is not clear if you want to copy those 8 characters into a new buffer or if you are looking to do something else.
I can imagine that perhaps you simply want to display these 8 characters to a console user. The following code accomplishes this (but probably requires a bit of explanation following):
char *salt = "$1$12345678$blahblahblah";
int from = 3;
int to = 10;
int len = to-from+1;
printf("%.*s\n", len, salt+from);
Printf() and its variants have some pretty powerful string generation/manipulation capabilities. In this case I used a "precision" specifier provided as a parameter for the 's' conversion. If a precision is given for a 's' conversion it is taken to limit the number of characters emitted. I used 'len' via the '*' character to provide this limit in a parametrized fashion. If you know that you are always going to want to emit 8 characters starting from the 3rd (in C we always count from 0 not from 1) then your code can be simplified to the more straightforward form given below:
char *salt = "$1$12345678$blahblahblah";
printf("%.8s\n", salt+3);
Finally, sprintf can be used to copy to another buffer. I'll give a simple form of this task below (note that I changed variable names for clarity; you are extracting the salt from a line of password text):
char *password = "$1$12345678$blahblahblah";
char salt[9]; /* Size of 9 required to hold terminating NULL byte */
sprintf(salt, "%.8s\n", password+3);
/* Now you can use salt for various purposes */
printf ("The salt is %s\n", salt);

Resources