C中的字符串 [英] String in C

查看:62
本文介绍了C中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好Guyz,

有谁知道如何用不同类型的变量制作一个字符串(在C程序中)?我有一个名为x的int数组和一个名为y的字符串数组。我需要以下列形式创建一个字符串:


y [0] x [0] y [1] x [1] .....(例如:a1u3b2%3# 8 ...)

我试图使用strcat,但它给出了错误,说我的y数组是int,它必须是char。有人可以帮助我PLZ ...有没有人知道如何创建一个可以包含int和字符串变量的数组?


Palm。

Hi Guyz,
Does anyone know how to make a string (in C program) out of different types of variables? I have an int array called x and a string array called y. I need to make a string in the form of :

y[0]x[0]y[1]x[1]..... (Eg: a1u3b2%3#8...)

I tried to use strcat, but it''s giving error saying my y array is int and it has to be char. Can some one help me plz... Does any one know how to create an array that can contain both int and string variables?


Palm.

推荐答案


你好Guyz,

有谁知道如何用不同类型的变量创建一个字符串(在C程序中)?我有一个名为x的int数组和一个名为y的字符串数组。我需要以下列形式创建一个字符串:


y [0] x [0] y [1] x [1] .....(例如:a1u3b2%3# 8 ...)

我试图使用strcat,但它给出了错误,说我的y数组是int,它必须是char。有人可以帮助我PLZ ...有没有人知道如何创建一个可以包含int和字符串变量的数组?



Palm。
Hi Guyz,
Does anyone know how to make a string (in C program) out of different types of variables? I have an int array called x and a string array called y. I need to make a string in the form of :

y[0]x[0]y[1]x[1]..... (Eg: a1u3b2%3#8...)

I tried to use strcat, but it''s giving error saying my y array is int and it has to be char. Can some one help me plz... Does any one know how to create an array that can contain both int and string variables?


Palm.



sprintf想要你想要吗?

Does sprintf do want you want?


Hi Arne,

感谢您的快速回复,但不幸的是,sprintf似乎没有工作。它保存了我的int值,但没有保存字符%。当我尝试保存%%时,它只是保存空格而不是%符号。这就是我的尝试:


char * final ="" ;;

sprintf(final," %%","%i" ;,x [0]);


如果我只说sprintf(final,%i,x [0]),它会在我的数组中保存'z' ,但是当我试图保存%符号时,它会保存为null,但它适用于其他字符,如#或@ ...我该如何解决这个问题呢?


Palm。
Hi Arne,
Thanks for your quick response, but unfortunately sprintf doesn''t seem working. It''s saving my int value, but not the character "%". When I tried to save "%%", it''s just saving blank space, not the % sign. This is what I tried:

char *final="";
sprintf(final,"%%","%i", x[0]);

if i just say sprintf(final,"%i",x[0]), it saves what''z in my array x, but when i tried to save % sign, it''s saving null, but it''s working for other characters like # or @... How can I fix this problem?

Palm.


你的意思是这样的:


char str [10];

char a [ ] ="%";

char b [] ="#" ;;

char c [] =" @" ;;

char d [] ="" ;;

strcpy(str,d);

strcat(str,a);

strcat(str,b);

strcat(str,c);

strcat(str,d);

cout< < str<< endl;


??
Do you mean like this:

char str[10];
char a[] = "%";
char b[] = "#";
char c[] = "@";
char d[] = "";
strcpy(str, d);
strcat(str, a);
strcat(str, b);
strcat(str, c);
strcat(str, d);
cout << str << endl;

??


这篇关于C中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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