Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

๐Ÿ”ข C Program to Print Hollow Continuous Number Square ๐Ÿ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } ๐Ÿ“‹ Copy Code ๐Ÿ’ป Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ๐Ÿ”ข C Program to Print Standard Hollow Binary Row Square ๐Ÿ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

write a program to implement atoi function in c

Description:

    • int my_atoi(const char *s)
      • The function will recieve a string and covert the number stored in the string into exact integer number.
      • Return the number.
Pr-requisites:-
    • Functions
    • Pointers

Objective: -

  • To understand the concept of
    • Functions and Pointers
Inputs: -
          String, String and Integer

Sample execution: -

Test Case 1 :
Enter a numeric string: 12345

String to integer is 12345

Test Case 2 :
Enter a numeric string: -12345

String to integer is -12345

Test Case 3 :
Enter a numeric string: +12345

String to integer is 12345

Test Case 4 :
Enter a numeric string: +-12345

String to integer is 0

Test Case 5 :
Enter a numeric string: 12345-

String to integer is 12345

Test Case 6 :
Enter a numeric string: abcd12345

String to integer is 0

Test Case 7 :
Enter a numeric string: 12345abcd

String to integer is 12345


PROGRAM

----------------------------------------------------------------------------------------------------------------------------


#include <stdio.h>

int my_atoi(const char str[])

{

    int i=0,a=0,b=0,t=0;

    while(str[i]!='\0')

    {

        if(str[0]=='*')

        {

            break;

        }

        if((str[0]=='-' && str[1]=='+') || (str[0]=='+' && str[1]=='-') || (str[0]=='+' && str[1]=='+') || (str[0]=='-' && str[1]=='-'))

        {

            break;

        }

        if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))

        {

            break;

        }

        if(str[0]=='-')

        {

            t=1;

         //   i++;

        }

        if(str[i]>='0' && str[i]<='9')

        {

            b=str[i]-48;

            a=a*10+b;

            

        }

        i++;

    }

    if(t==1)

    return -a;

    else

    return a;

}

int main()

{

    char str[20];

      printf("Enter a numeric string : ");

    scanf("%s", str);

    int res = my_atoi(str);

    printf("String to integer is %d\n", res);

}

---------------------------------------------------------------------------------------------------------------

 

Comments

Popular Posts

๐ŸŒ™