Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have made a function and im adding 1 to the string im giving as input but function is not returning complete string ......
#include <stdio.h>
#include <string.h>
char *return_and_pass(char x[])	// OR char return_and_pass(char *s)
    {
         int i,carry;
    i=0;
    x[i]=x[i] + 1;
    carry=x[i]/10;
    i++;
   while(carry>0)
  {   x[i]=x[i] + carry;
      carry=x[i]/10;
       if(x[i]>9)
    { x[i]=x[i]%10;
        i++;}
  }
     return x;
    //return it
    }
     
int main()
    {
    	char a[10];
    	gets(a);
    	puts(return_and_pass(a));
    	return 0;
    }


what im getting as output:-
when im entering a string "123"
it shows me output as only 2....

where is the problem in my code?

What I have tried:

i have tried this code...
#include <stdio.h>
#include <string.h>
char *return_and_pass(char x[])	// OR char return_and_pass(char *s)
    {
         int i,carry;
    i=0;
    x[i]=x[i] + 1;
    
  }
     return x;
    //return it
    }
whem in 
    
int main()
    {
    	char a[10];
    	gets(a);
    	puts(return_and_pass(a));
    	return 0;
    }


when im using this.....it gives correct output but when i add the while loop to the code as in the previous i hve given it gives wrong answer.....???

ans should be 223.
Posted
Updated 26-Jun-18 0:46am

Look at your code.
You add one to the FIRST character in the string:
x[i]=x[i] + 1;
So the string becomes "223". Then you work out your carry:
carry=x[i]/10;
which ... doesn't do what you think it does...
x[i] is a char value, and contains '2' - that is not the same a the value 2, it's a character, so it actually has the value 50 as a number: Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion[^]. So when you divide that by ten, you get 5.
The rest of your code just messes up the string because you haven't realised what the difference between characters and numbers are.

Now, I have no idea what that code is supposed to do, so I definitely can't fix it for you - and wouldn't if I could, this is your homework after all, not mine. So I would suggest you go back to eth original problem statement your tutor gave you and read it again, really carefully - this code will almost certainly not do anything like what you want it to!
 
Share this answer
 
v2
Comments
Member 13887543 26-Jun-18 10:51am    
This is my modified code. Im having problem with numbers like 99,199,999... in which no. of 9's are greater than 2 like in 99 im getting output as
00 but not 100.......and im getting segmentation fault with these numbers with numbers my output is correct....what do i need to correct in my code???

#include <stdio.h>
#include <string.h>
char *return_and_pass(char x[]) // OR char return_and_pass(char *s)
{ int i,carry,l=strlen(x);
i=0;
if(x[i]<'9')
{ x[i]=x[i]-'0' + '1';
puts(x);}
else
{
x[i]='0';
carry=1;
i++;
while(carry>0)
{ if(x[i]='9')
{ x[i]='0';
carry=1;
i++;
if(i==l)
{x[i]='1';}
}
else
{ x[i]=x[i] - '0' + '1';
carry=(x[i]-'0')/10;
if(x[i]>'9')
{ x[i]=(x[i]-'0')%10;
i++;}
}
}
}

puts(x);
return x;
//return it
}

void rev(char x[])
{
int i,carry,l=strlen(x);
char k;
for(i=0;i<l/2;i++)
{
k=x[i];
x[i]=x[l-i-1];
x[l-i-1]=k;
}
}


int main()
{
char a[10];
gets(a);
rev(a);

puts(return_and_pass(a));
return 0;
}
Member 13887543 26-Jun-18 10:53am    
**edit**
in line 3rd "with other *numbers"
OriginalGriff 26-Jun-18 11:07am    
So what have you tried to do to find out why?
Have you used the debugger?
Did you try this algorithm manually before you started coding?
Hint: No, you didn't - which was a big mistake. Why? Because you are working from the wrong end. Think about it: if I give you a number like 12345 and you add one to it, do you expect to start by adding 1 to the 1 or the 5?

Here is what I would do: write a function which takes a pointer to a character and an integer, and which returns an integer. The function adds the integer to the character at the pointer, normalizes it to a digit, and returns 1 if the character was greater than 9 before normalization. Other wise it returns zero. That 0 or 1 value is your "carry" to the next digit. Get that working - and it's pretty simple - and you can call it in a loop to do all the digits, regardless of how many there are. Just start from the least significant digit.
Patrice T 26-Jun-18 18:16pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Member 13887543 26-Jun-18 14:49pm    
Thnx it really helped
You are working with string representation of 123, so you are actually working with the values 31, 32, 33 (ASCII (HEX) values of the char '1', '2', '3')...
So no way you would get the 'right' value...
Without understanding the reasons for such an obscure code... do this:
1. Reverse the string
2. Convert it to numeric
3. +1
4. Convert to string
5. Reverse it
 
Share this answer
 
v2
Comments
OriginalGriff 26-Jun-18 6:46am    
*cough* hex *cough*
Kornfeld Eliyahu Peter 26-Jun-18 6:53am    
Come on! You have to leave something to the young!!!
OriginalGriff 26-Jun-18 6:59am    
LOL.
Member 13887543 26-Jun-18 14:48pm    
thanx for ur help. it worked.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900