Google Interview Question
Software Engineer / DevelopersI guess this is what they need?
bool IncIntChar(char*a,int s)
{
bool flag=0;
if(*(a+1)!=0)
{
flag=IncIntChar(a+1,s);
}
if(strlen(a)<s)
{
if(strlen(a)==1)
{
if(*a=='9') {
*a= '0';
flag=1;
}
else *a=*a+1;
}
else
{
if(flag)
{
if(*a=='9') {
*a= '0';
flag=1;
}
else
{
*a=*a+1;
flag=0;
}
}
}
}
else
{
if(flag==1) *a='1';
else *a='0';
printf("%s\n",a);
}
return flag;
}
Was atoi and itoa not allowed? Otherwise....
char *charIncr (char *myStr) {
int myInt = atoi(myStr)+1;
myStr[0] = '\0'; //Can be ignored
int strLen = strlen(myStr);
char *newStr = (char*)malloc(sizeof(char)*(strLen+1));
itoa(myInt,newStr,10);
return newStr;
}
char *addStringByOne(const char *src) {
if(!src)
return NULL;
size_t len = strlen(src);
int i, j;
int base, tmp;
char t;
char *ret = (char*)malloc(len + 2);
if(!ret)
return NULL;
for(i = len - 1; i >= 0; i--) {
ret[len - 1 - i] = src[i];
}
tmp = 1;
for(i = 0; i < len; i++) {
base = ret[i] - '0' + tmp;
ret[i] = (base % 10) + '0';
tmp = base / 10;
}
if(tmp) {
ret[i++] = tmp + '0';
}
ret[i] = '\0';
--i;
for(j = 0; j < i; j++, i--) {
t = ret[j];
ret[j] = ret[i];
ret[i] = t;
}
return ret;
}
#include<stdio.h>
#include<string.h>
main()
{
int i,l,j;char s[1000];
scanf("%s",s);
l=strlen(s);
j=l-1;
if(s[j]=='9')
{
while( j>=1)
{
if(s[j]=='9')
s[j]='0';
else break;
j--;
}
if(s[0]=='9')
{
s[0]='1';s[l]='0'; s[l+1]='\0';
}
else
s[j]=s[j]-'0'+1+48;
}
else
s[l-1]=s[l-1]-'0'+1+48;
printf("%s",s);
}
Allocate an array of MAX_NUM_OF_DIGITS and memset it with '0'
start from the end of array and assign it '\0'.
Have a pointer print_ptr to the at last but one position in the array. We can use this for printing the array any given time and as a pointer to most significant digit.
Rest is usual increment the counter and check if the digit reached '9' then we move left. and if the we reached the msb then we need to move the print_ptr left too.
Once the print_ptr reaches the array[0] and the value reaches '9' and a request to increment comes in we just reset the whole procedure and start from the begining
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int sum(char *s)
{
- int carry;
| if (!*s)
- return 1;
| carry = sum(s+1);
| if(carry){
- if(*s=='9'){
- *s = '0';
3 return 1;
2 }
2 else {
- *s = *s + 1;
3 return 0;
2 }
| }
| return 0;
}
int main(int argc,char* argv[])
{
char *s = argv[1];
int c = sum(s);
- if(c) {
- char *t = (char*)malloc(strlen(s)+1);
2 strcpy(t+1,s);
2 *t= '1';
| printf("%s",t);
| return 0;
| }
| printf("%s",s);
}
char increment(char *number, int len)
{
int c = 1; /* possible carry return */
if( len <= 0 )
return '\0'; /* huh? */
while( c && len > 0 ) /* got something to add to a valid string */
{
sum = number[len-1] - '0' + c;
if( sum > 10 )
{
sum = sum%10;
c = sum/10;
}
else
{
c = 0;
}
number[len-1] = sum + '0';
len --;
}
return c + '0';
}
char *increment(char *str)
{
int k;
int add = 1;
int l = strlen(str);
if (l == 0) return NULL;
while(True && l>0)
{
k = char2int(str[l-1]);
k = k + add;
str[l-1] = int2char(k%10); //Update the Number
add = k/10;
if(add == 0) //If no carry over
break;
else
{
l--;
}
}
if(l==0 && add !=0)
{
char *new_str = (char*)malloc(sizeof(char)+1);
new_str[0] = int2char(add);
for(int i=0;i<l; i++)
{
new_str[i+1] = str[i];
}
return new_str;
}
return str;
}
Increment the rightmost digit and keep passing the ten's digit leftwards as long as there is one. Prepend a '1' if it moves beyond the 1st digit.
- Jagat July 28, 2011