Compare String Using Conditional Statements

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to use the strcmp() function in C language to compare two strings and perform different operations based on the comparison result.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-136079{{"`Compare String Using Conditional Statements`"}} c/variables -.-> lab-136079{{"`Compare String Using Conditional Statements`"}} c/data_types -.-> lab-136079{{"`Compare String Using Conditional Statements`"}} c/operators -.-> lab-136079{{"`Compare String Using Conditional Statements`"}} c/if_else -.-> lab-136079{{"`Compare String Using Conditional Statements`"}} c/user_input -.-> lab-136079{{"`Compare String Using Conditional Statements`"}} c/memory_address -.-> lab-136079{{"`Compare String Using Conditional Statements`"}} c/function_declaration -.-> lab-136079{{"`Compare String Using Conditional Statements`"}} end

Compare String Using Conditional Statements

In this lab, you will learn how to compare strings using conditional statements.

  1. Create a file named conditional-statements.c and open it in WebIDE.

  2. Copy the following code into the file:

    #include <stdio.h>
    #include <string.h>
    
    void main(){
      int n1, n2, result;
      char operator[10];
    
      printf("Enter first number: ");
      scanf("%d",&n1);
    
      printf("Enter second number: ");
      scanf("%d",&n2);
    
      printf("Enter operation name (add or sub): ");
      scanf("%s",operator);
    
      if(strcmp(operator,"add") == 0)
        result = n1 + n2;
      else if(strcmp(operator,"sub") == 0)
        result = n1 - n2;
      else
        result=0;
    
      printf("The result is : %d\n\n\n",result);
    }
  3. Save the file.

  4. Compile the code using the following command in the terminal:

    $ gcc conditional-statements.c -o conditional-statements
  5. Run the compiled program using the following command:

    $ ./conditional-statements
  6. Enter the first number when prompted and press enter.

  7. Enter the second number when prompted and press enter.

  8. Enter the operation name (add or sub) when prompted and press enter.

  9. The program will compare the entered operation name to perform either addition or subtraction based on the comparison result.

  10. The program will then display the result.

Summary

After completing this lab, you will be able to compare strings using conditional statements in C and perform different operations based on the comparison result.

Other C Tutorials you may like