Evaluate a String with a Switch in C++

Why can't the switch statement be applied to strings?

The reason why has to do with the type system. C/C++ doesn't really support strings as a type. It does support the idea of a constant char array but it doesn't really fully understand the notion of a string.

In order to generate the code for a switch statement the compiler must understand what it means for two values to be equal. For items like ints and enums, this is a trivial bit comparison. But how should the compiler compare 2 string values? Case sensitive, insensitive, culture aware, etc ... Without a full awareness of a string this cannot be accurately answered.

Additionally, C/C++ switch statements are typically generated as branch tables. It's not nearly as easy to generate a branch table for a string style switch.

Switch statement to evaluate string input C++

if perfomance not an issue, I just would use std::count_if:

int upps = std::count_if( pass.begin(), pass.end(), isupper );
int digs = std::count_if( pass.begin(), pass.end(), isdigit );

working example on ideone

can we use switch-case statement with strings in c?

No, you can't. Switch is intended to compare numeric types, and for extension char types.
Instead you should use the strcmp function, included in string header:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char * argv[]) {
if (argc != 4) {
puts("Incorrect usage");
return 1;
}
/* You should check the number of arguments */

char * op = argv[1];
int a = atoi(argv[2]);
int b = atoi(argv[3]);
/* You should check correct input too */

if (strcmp(op, "+") == 0)
printf("%d + %d = %d\n", a, b, a + b);
else if (strcmp(op, "-") == 0)
printf("%d - %d = %d\n", a, b, a - b);
/* Add more functions here */

return 0;
}

How to properly use C switch statement

You cant use a switch in C with a String. What you are doing now is using the pointer to the first char in the array str in the switch. Thats why always goes to the default section.
A good way to do what you want would be using strcmp:
int strcmp(const char *str1, const char *str2);
if Return value < 0 then it indicates str1 is less than str2.

if Return value > 0 then it indicates str2 is less than str1.

if Return value = 0 then it indicates str1 is equal to str2.

char str2[]="e";
if(strcmp(str, str2))
printf("Your string is: %s \n", str);
else
break;

Also don't use while(0==0), use while(1) instead

Evaluating a switch expression at runtime in C

See https://en.cppreference.com/w/c/language/switch

The syntax requires

case constant_expression : statement

with

constant_expression - a constant expression of the same type as the type of condition after conversions and integral promotions

I.e. syntax requires constant expression.

The result of a function call is not a constant expression.

That is why it is illegal.

Switch statement using string on an array

Switch statements in C aren't smart like one's found in other languages (such as Java 7 or Go) you cannot switch on a string (Nor can you compare strings with ==). Switch can only operate on integral types (int, char, etc).

In your code you call switch with: switch(name[20]). That means switch(*(name + 20)). In other words switch on the 21st char in name (because name[0] is the first). As name only has 20 chars you are accessing whatever memory is after name. (which could do unpredictable things)

Also the string "kevin" is compiled to a char[N] (where N is strlen("kevin") + 1) which contains the string. When you do case "kevin". It will only work if name is in the exact same piece of memory storing the string. So even if I copied kevin into name. It still would not match as it is stored in a different piece of memory.

To do what you seem to be trying you would do this:

#include <string.h>
...
if (strcmp(name, "kevin") == 0) {
...
}

String compare (strcmp) returns different values based on the difference in the strings. Eg:

int ord = strcmp(str1, str2);
if (ord < 0)
printf("str1 is before str2 alphabetically\n");
else if (ord == 0)
printf("str1 is the same as str2\n");
else if (ord > 0)
printf("str1 is after str2 alphabetically\n");

Side note: Dont use scanf("%s", name) in that form. It creates a common security problem use fgets like this: (there is a safe way to use scanf too)

#define MAX_LEN 20
int main() {
char name[MAX_LEN];
fgets(name, MAX_LEN, stdin);
...


Related Topics



Leave a reply



Submit