I am trying to compare two specific characters in two strings,but I want to make the comparing without case sensitivity. How can I do this?
right now I am using a code like that:
if (str1[i]==str2[j]) printf("Equal");
but I want to do this without case sensivity.
Thank you in advance for taking your time to help!
You can use low case for both chars, for example by using tolower function:
if (tolower(str1[i])==tolower(str2[j])) printf("Equal");
Also keep in mind: tolower does not work for multibyte char. So for those chars you should use other function
We can achieve your requirement by converting both of the character to either upper or lower case character using toupper() or tolower().
Example:
#include <stdio.h>
#include <ctype.h> //For tolower()
int main()
{
char str1[]="Time", str2[]="time";
/*
* Just for an example i am comparing the first char
* from 2 different strings.
*/
if(tolower(str1[0]) ==tolower(str2[0])) {
printf("Char's are equal\n");
}
else {
printf("Char's are not equal");
}
return 0;
}
Output:
Char's are equal
Related
I want to print out a polynomial expression in c but i don't know print x to the power of a number with printf
It's far from trivial unfortunately. You cannot achieve what you want with printf. You need wprintf. Furthermore, it's not trivial to translate between normal and superscript. You would like a function like this:
wchar_t digit_to_superscript(int d) {
wchar_t table[] = { // Unicode values
0x2070,
0x00B9, // Note that 1, 2 and 3 does not follow the pattern
0x00B2, // That's because those three were common in various
0x00B3, // extended ascii tables. The rest did not exist
0x2074, // before unicode
0x2075,
0x2076,
0x2077,
0x2078,
0x2079,
};
return table[d];
}
This function could of course be changed to handle other characters too, as long as they are supported. And you could also write more complete functions operating on complete strings.
But as I said, it's not trivial, and it cannot be done with simple format strings to printf, and not even to wprintf.
Here is a somewhat working example. It's usable, but it's very short because I have omitted all error checking and such. Shortest possible to be able to use a negative float number as exponent.
#include <wchar.h>
#include <locale.h>
wchar_t char_to_superscript(wchar_t c) {
wchar_t digit_table[] = {
0x2070, 0x00B9, 0x00B2, 0x00B3, 0x2074,
0x2075, 0x2076, 0x2077, 0x2078, 0x2079,
};
if(c >= '0' && c <= '9') return digit_table[c - '0'];
switch(c) {
case '.': return 0x22C5;
case '-': return 0x207B;
}
}
void number_to_superscript(wchar_t *dest, wchar_t *src) {
while(*src){
*dest = char_to_superscript(*src);
src++;
dest++;
}
dest++;
*dest = 0;
}
And a main function to demonstrate:
int main(void) {
setlocale(LC_CTYPE, "");
double x = -3.5;
wchar_t wstr[100], a[100];
swprintf(a, 100, L"%f", x);
wprintf(L"Number as a string: %ls\n", a);
number_to_superscript(wstr, a);
wprintf(L"Number as exponent: x%ls\n", wstr);
}
Output:
Number as a string: -3.500000
Number as exponent: x⁻³⋅⁵⁰⁰⁰⁰⁰
In order to make a complete translator, you would need something like this:
size_t superscript_index(wchar_t c) {
// Code
}
wchar_t to_superscript(wchar_t c) {
static wchar_t huge_table[] {
// Long list of values
};
return huge_table[superscript_index(c)];
}
Remember that this cannot be done for all characters. Only those whose counterpart exists as a superscript version.
Unfortunately, it is not possible to output formatted text with printf.
(Of course one could output HTML format, but this then would need to be fed into an interpreter first for correct display)
So you cannot print text in superscript format in the general case.
What you have found is the superscript 1 as a special character. However this is only possible with 1 and 2, if I remember correctly (and only for the right code-page, not in plain ASCII).
The common way to print "superscripts" is to use the x^2, x^3 syntax. This is commonly understood.
An alternative is provided by klutt's answer. If you switch to unicode by using wprintf instead of printf you could use all superscript characters from 0 to 9. Even though, I am not sure how multi-digit exponents look like in a fixed-width terminal it works in principle.
If you want to print superscript 1, you need to use unicode. You can combine unicode superscripts to write a multi-digit number.
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main() {
setlocale(LC_CTYPE, "");
wchar_t one = 0x00B9;
wchar_t two = 0x00B2;
wprintf(L"x%lc\n", one);
wprintf(L"x%lc%lc\n", one, two);
}
Output:
$ clang ~/lab/unicode.c
$ ./a.out
x¹
x¹²
Ref: https://www.compart.com/en/unicode/U+00B9
char *x="Çankırı";
char *y=malloc(sizeof(char)*25);
scanf("%s",y);
if(strcmp(x,y) == 0)
printf("A");
else
printf("%s",y);
I enter 'Çankırı' for y, but else part runs. How can I compare these strings?
Windows10 , codeblocks.
EDIT: I found a clue. Problem is about setlocale function. When I use setlocale(LC_ALL,"TURKISH"), one of the string doesn't work fine(Output is not Çankırı, Ank2r2), and If I use setlocale(LC_ALL,"C"), other one doens't work fine. I don't know how to fix it.
You probably need to use functions that accept 'wide' characters. For example:
#include <wchar.h>
wchar_t *x=L"Çankırı";
wchar_t y[25];
wscanf(L"%s",y);
if(wcscmp(x,y) == 0)
wprintf(L"A");
else
wprintf(L"%s",y);
I am implementing a lexicographic sort and my professor told us to use strcmp in our implementation. The problem is, strcmp is very confusing in respects to how it compares strings.
For instance, this here yields false:
if (strcmp("What", "am") > 0) {
printf("true\n");
} else {
printf("false\n");
}
Isn't "What" suppose to be greater than "am" lexicographically? The man page is very spartan in terms of explaining how the function determines if one string is greater or less than the other. There are some questions here, but I still cannot determine this result based on those explanations.
The problem is that strcmp makes a binary comparison. This fact makes the function case sensitive! The ASCII code of "W" is smaller than the ASCII code of "a".
The way to solve the problem is to compare text strings that as the same capitalization.
A simple way to obtain this shall be:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char* stoupper( char* s )
{
char* p = s;
while (*p) {
*p= toupper( *p ); p++
};
return s;
}
int main(void)
{
char a[10];
char b[10];
strcpy(a,"What");
strcpy(b,"am");
if (strcmp(stoupper(a), stoupper(b)) > 0) {
printf("true\n");
} else {
printf("false\n");
}
}
Remember that the use of the function stoupper modifies definitively the text in the string!
Any capital letter compare less than any lowercase one. Look at an ASCII chart and the definition of lexicographical comparison.
My guess is that this is because 'W' comes before 'a' in the ascii table. So 'What' is actually 'lesser' than 'am'.
If you switch them I guess 'am' is 'greater' than 'What'.
I'm trying to read a string from stdin and substitute the vowels on the string by '_'.
Off course it's simple, but I want to substitute the accented vowels too. Obviously the following code doesn't work for those cases. I've tryied several things, like read char by char but and it to a short, consider the cases where line[i]<0, etc.
while(fgets(line, 250, stdin)){
size=strlen(line);
for(i=0;i<size;i++){
if(line[i]==65 || line[i]==69 ||line[i]==73 ||line[i]==79 ||line[i]==85 ||line[i]==97 ||line[i]==101 ||line[i]==105 ||line[i]==111 ||line[i]==117){
line[i]='_';
}
}
}
Any suggestions? Thank you in advance.
Ps: Consider the used encoding is Western (ISO-8859-1) -> http://www.alanwood.net/demos/ansi.html
You are probably running into a signed vs unsigned character issue. The ANSI values for the accented characters show on the link you provided are larger that the max size of an signed char. This is probably why you are seeing negative character values as well.
If you want to fix this properly, I'd suggest using a wide character format and character constants.
If you want something quick and easy, add the following in your for loop to see how the characters are mapping to values:
printf( "%c %d\n", line[i], (int)line[i] );
Then add the appropriate values to your if.
I used the following data
htyuàsdsècvcvcàdìssd
and the following code works with accented characters. The result was htyu_sds_cvcvc_d_ssd
#include <stdio.h>
#include <wchar.h>
#include <locale.h>
int main(int argc, char **argv){
setlocale(LC_CTYPE, "UTF-8");
FILE *f = fopen("/Users/sandeepshabd/objectiveC/C_TEST_App/C_TEST_App/file.txt", "r, ccs=UTF-8");
if (!f)
return 1;
for (wchar_t c; (c = fgetwc(f)) != WEOF;){
switch (c) {
case L'à': c=L'_'; break;
case L'è': c=L'_';break;
case L'é': c=L'_';break;
case L'ì': c=L'_';break;
case L'ò': c=L'_';break;
case L'ù': c=L'_';break;
default: break;
}
wprintf(L"%c", c);
}
fclose(f);
return 0;
}
So basically I wanted to create a program in C, in wich you would input 2 character long string (mix of letter and noumber ex.r1,u2,i3,i4,r6) to be the input in my program. Later I want to put this string in SWITCH. Is this possible?
Here's my simple sourcecode. Please correct me on any mistakes :)
#include <stdio.h>
int main(void)
{
char string[2];
scanf("%s", &string);
switch (string)
{
case 'u1' :printf("%s\n", string);break;
default :printf("ERROR");break;
}
return 0;
}
Create a code based on the string and switch on that.
#define Code(a,b) (a + 256*b)
char string[3]; // 3 not 2
if (scanf("%2s", string) != 1) { // No &
Handle_Error();
}
int scode = Code(string[0], string[1]);
switch (scode) {
case Code('u', '1') : printf("%s\n", string); break;
case Code('r', '2') : printf("r2\n"); break;
...
default :printf("ERROR");break;
}
A switch(x) needs an integer value for x and string is an array. So the original approach will not work.
The program can use an integer based on the string for x and use the same method for generating the case values. Since there is only the first 2 char of the string are of interest, the int value is unique.
No, this is not possible. Switch only works with integral types in C (int, short, long, etc, as well as types defined with enum).
You can however use a simple if-else construct to get the same behavior:
if (strcmp(string, "ui" ) == 0) //test for string equality
{
printf("%s\n", string);
}
else
{
printf("ERROR")
}
We use strcmp instead of == because we are dealing pointers which almost certainly not compare equal even when the two strings have the same content.
strcmp(str1, str2) == 0 is the standard idoim in C for comparing two strings.
strcmp returns an integer representing how two strings compare to each other. 0 means they are equal, a negative number means that the first string is lexographically "less than" the second, and a positive number means that the first string is lexographically "greater than" the second. More info can be found here.
A switch won't work here.
You need to use an if/else if construct and strcmp to compare the strings.
Also, you need at least 3 characters in your input array so that it can hold the two input characters and the terminating null character.
Of course, such a small buffer can easily overflow.