Home > Net >  atoi function in C
atoi function in C

Time:02-06

Question in C : given a number in character array , find the biggest two digit number (group of two digits should be checked from left to right) with the condition that the number cannot have same two digits . eg. input: 64998434 , output: 84 (not 99).

Solution : atoi expects string not character array . therefore just increase the array length by 1 and initialize it with terminating character as written below

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

int main()
{
    char no[] = "64998434";
    int big = 0, temp, j = 0, length;

    length = strlen(no);
    //char str[2] = "";  // not working
    char str[3] = "\0";  // working

    if (length <= 0 || length == 1)
    {
        printf("Invalid input");
        return 0;
    }
    else if (length == 2)
    {
        printf("%d", atoi(no));
        return 0;
    }


    //loop to get two digits from input array and store in another array to convert into 
    //integer and than save and check for bigger number

    for (int i = 0; i < length; i  = 2)
    {
        str[j] = no[i];
        str[j   1] = no[i   1];
        if (str[0] != str[1])
        {
            temp = atoi(str);
            if (temp > big)
            {
                big = temp;
            }
        }
        j = 0;
    }

    if (big != 0)
    {
        printf("%d\n", big);
        return 0;
    }
}

enter image description here

CodePudding user response:

str is an array of length 2. You store potentially non-null characters in both of the array slots (j is always 0 in your code):

    str[j] = no[i];
    str[j   1] = no[i   1];

Then you pass it to atoi, which expects a null-terminated string. But str now has no null character anymore and therefore the behavior is undefined.

str should have length 3, not 2.

(This may not be the only issue with your code.)

CodePudding user response:

Edit: to clarify , number was supposed to be searched from left to right in a group of two digits , therefore output should be 84 not 98.(since my question was not properly understandable)

Correction : atoi expects string ,not character array [ this solution is not using atoi function]


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

int convert(char *s, int l)
{
    // Initialize a variable
    int num = 0;
    // int n = l;

    // Iterate till length of the string
    for (int i = 0; i < l; i  )

        // Subtract 48 from the current digit
        num = num * 10   (s[i] - 48);

    return num;
}

int main()
{
    char no[] = "64998434";
    int big = 0, temp, j = 0, length, num, num2;
    for (int i = 0; no[i] != '\0'; i  )
    {
        length = i;
    }

    char str[2] = "";

    if (length <= 0 || length == 1)
    {
        printf("Invalid input");
        return 0;
    }
    else if (length == 2)
    {
        // printf("%d", atoi(no));
        // return 0;
        num2 = convert(no, length);
        printf("%d", num2);
    }

    for (int i = 0; i < length; i  = 2)
    {
        str[j] = no[i];
        str[j   1] = no[i   1];
        if (str[0] != str[1])
        {
            temp = convert(str, 2);
            if (temp > big)
            {
                big = temp;
            }
        }
        j = 0;
    }

    if (big != 0)
    {
        printf("%d", big);
        return 0;
    }
}

enter image description here

CodePudding user response:

atoi does not seem required for this task.

Here is a simple solution using strncmp():

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

int main() {
    char str[] = "649984343497864236666784328253489";
    int i, imax = -1, len = strlen(str);
    for (i = 0; i   2 <= len; i  = 2) {
        if (str[i] != str[i   1] && (imax < 0 || strncmp(str   i, str   imax, 2) > 0)) {
            imax = i;
        }
    }
    if (imax < 0)
        printf("no solution\n");
    else
        printf("%.2s\n", str   imax);
    return 0;
}

CodePudding user response:

Seems like a single pass should do it.

// Return -1 on error
// Otherwise return the value of the biggest pair
int biggest_pair(const char *s) {
  int biggest = -1;
  while (s[0]) {
    // This and next character a digit?
    if (!(s[0] >= '0' && s[0] <= '9' && s[1] >= '0' && s[1] <= '9')) {
      return -1;
    }
    int test = (s[0] - '0')*10   s[1] - '0';
    // Are digits different and this is larger than the current best?
    if (s[0] != s[1] && test > biggest) {
      biggest = test;
    }
    s  = 2;
  }
  return biggest;
}

Code could use isdigit().

int biggest_pair(const char *s) {
  const char *us = (const char *) s;
  int biggest = -1;
  while (us[0]) {
    // Check this and next character as digits?
    if (!(isdigit(us[0]) && isdigit(us[1])) {
      return -1;
    }
    int test = (us[0] - '0')*10   us[1] - '0';
    // Are digits different and this is larger than the current best?
    if (us[0] != us[1] && test > biggest) {
      biggest = test;
    }
    us  = 2;
  }
  return biggest;
}

This 2nd version also properly distinguishes 0 from -9 on those rare non-2's complement machines.

  •  Tags:  
  • Related