如何让函数返回完整的字符串 [英] How do I get the function to return complete string

查看:104
本文介绍了如何让函数返回完整的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个函数并且我在字符串中添加了1作为输入,但函数没有返回完整的字符串......

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;
    }





我输出的是什么: -

当我输入一个字符串123

它显示我只输出2 ....



我的代码中的问题在哪里?



我尝试了什么:



i试过这段代码。 ..



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;
    }





当我使用这个.....它提供了正确的输出但是当我添加while循环到如前所述的代码给出了错误答案..... ???



ans应为223.



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.

推荐答案

查看你的代码。

你在字符串中的第一个字符加一个:

Look at your code.
You add one to the FIRST character in the string:
x[i]=x[i] + 1;

所以字符串变为223。然后你计算你的随身携带:

So the string becomes "223". Then you work out your carry:

carry=x[i]/10;

哪个...不符合你的想法...

x [i]是一个char值,包含'2' - 与值2不同,它是一个字符,因此它实际上具有值50作为数字:Ascii表 - ASCII字符代码和html,八进制,十六进制和十进制图表转换 [ ^ ]。因此,当你将它除以10时,你会得到5.

其余的代码只会弄乱字符串,因为你还没有意识到字符和数字之间有什么区别。



现在,我不知道该代码应该做什么,所以我绝对无法为你修复它 - 如果可以的话我也不会,这毕竟是你的功课, 不是我的。所以我建议你回到你的导师给你的eth原始问题陈述并再次阅读,非常仔细 - 这段代码几乎肯定不会做你想要的任何事情!

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!


您正在使用123的字符串表示,因此您实际上使用值31,32,33(char'1','2','3'的ASCII(HEX)值)...

所以你不可能得到'正确'的价值...

如果不理解这些模糊代码的原因......这样做:

1 。反转字符串

2.将其转换为数字

3. +1

4.转换为字符串

5.将其反转
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


这篇关于如何让函数返回完整的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆