Showing posts with label if-else. Show all posts
Showing posts with label if-else. Show all posts

How to remove any character from string in c

 In this I will tell you how to remove any character from the given String in C . So without any further delay let's get started .

Code:

#include<stdio.h>
#include<string.h>
//how to remove any character from string in c
int main(){
    char str[100];
printf("Enter the string \n");
    scanf("%[^\n]s",str);
  int j = 0;
    for(int i=0;str[i]!='\0';i++){
           if (str[i] == 'a')
        {
         str[j++]=str[i+1];  
         i++; 
        }
        else{
         str[j++]=str[i];
        }
        
    }
str[j]='\0';
    printf("%s  ",str);
    return 0;
}

Logic:

Now that we have see the code let's understand one by one . First of all we created a String of length 100 . And named it str . Then with the help of printf function we print user message to Enter the string . And then with help of scanf function  taking  String  as input. From the user .  And then we use a for loop and  run it till the end of the String. And we also declare variable j and initialize it from 0. For indexing of the same String after that character is removed . Then we take use   if-else  and in if condition we check for str[i]== 'a' if found then character after that is copied  in str[j++] else it copy before it is copied in str[j++]=str[i]  . And then after doing all this we end the String with '\0'.

Output:


And here you can see that our program work correctly and remove character 'a' from the String . Try this program by yourself and please provide me feedback by commenting your opinion . I hope that you find this helpful in your work. 

                                                              Thanks for reading  








Temperature Converter using C Language

 In this  I will tell you how to make temperature Converter using C language . So, without wasting   our  time let's get started with our today's  topic .  Let us first see the code then we understand logic and then    output.

Code:

#include <stdio.h>
int main()
{
    float inputTemp;
    char inputType;
    char outputType;
    float outputTemp;
    printf("Enter the unit for input (k for Kelvin, F for Fahernite ,C for Celcius)\n");
    scanf(" %c", &inputType); 
    printf("Enter the value for input \n");
    scanf("%f", &inputTemp);
    printf("Enter the unit for output (k for Kelvin, F for Fahernite ,C for Celcius)\n");
    scanf(" %c", &outputType);                //for output value
    if (inputType == 'c' || inputType == 'C') //when input type is Celsius
    {
        if (outputType == 'f' || outputType == 'F')
        {
            outputTemp=(inputTemp *9/5)+32;
            printf("Temperature in Fahernite : %.2f ", outputTemp);
        }
        else if (outputType == 'K' || outputType == 'k')
        {
            outputTemp = inputTemp + 273.15; // from Celcius to Kelvin
            printf("Temperature in Kelvin : %.2f", outputTemp);
        }
    }
    if (inputType == 'f' || inputType == 'F')
    { //when input type is Fahernite
        if (outputType == 'C' || outputType == 'c')
        {
            outputTemp = (inputTemp - 32) * 5 / 9; 
            printf("Temperature in Celcius: %.2f", outputTemp);
        }
        else if (outputType == 'K' || outputType == 'k')
        {
            outputTemp = (inputTemp - 32) * 5 / 9 + 273.15; 
            printf("Temperature in Kelvin: %.2f", outputTemp);
        }
    }
    if (inputType == 'K' || inputType == 'k')
    {  //when input is in Kelvin 
        if (outputType == 'F' || outputType == 'f')
        {
            outputTemp = (inputTemp - 273.15) * 9 / 5 + 32;
            printf("Temperature in Fahernite : %.2f", outputTemp);
        }
        else if (outputType == 'C' || outputType == 'c')
        {
            outputTemp = inputTemp - 273.15;
            printf("Temperature in Celcius: %.2f", outputTemp);
        }
    }
    return 0;
}

Logic:

Here we first define two variable of type float to take input Temperature and give output Temperature that is inputTemp and outputTemp. And then we define two variable of type Char to take Character of unit in which we want to give input to program(inputType) and then for output we use (outputType). And after taking these values we first check make all case that can be possible if input in Celsius  and in outputTemp variable we assign formula that we need to convert Celsius  into Fahrenheit and if user want output in Kelvin then we use formula to Celsius into Kelvin. And we did same if  input is in Kelvin  or in Fahrenheit . And alter only formula and done with our work . You can check for formula on internet . 

Output:




      now here you can see that our program work great . As we give 0 input it give output as we desired           in another unit .

     I hope this will help you in your work .

     Thanks for reading .