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;
}
}
str<\/code> is an array of length
2<\/code> .
You store potentially non-null characters in both of the array slots (
j<\/code> is always
0<\/code> in your code):
str[j] = no[i];
str[j + 1] = no[i + 1];
Edit:<\/strong> 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)
#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;
}
}
atoi<\/code> does not seem required for this task.
#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;
}
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';
if (s[0] != s[1] && test > biggest) {
biggest = test;
}
s += 2;
}
return test;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.