C和字符串 [英] C and strings

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

问题描述




我是c的新手,我遇到字符串有问题,任何人都可以给我一些

快速指针或指向好的c字符串教程?以下是我正在制作的

计划。


亲切的问候,

Anthony Irwin


#include< stdio.h>


char teststring [40] =" test string now";

char teststring1 [40] ;


int main(){

printf(" teststring =%s",teststring);

strcpy(teststring1 ,teststring);

printf(" \\\
teststring1 =%s",teststring1);


/ *如何为teststring分配新值说

*值我改变了字符串文本 * /


printf(" \ n\\\
II将teststring更改为=%s \ n",teststring);


返回0;

}

Hi,

I am new to c and am having trouble with strings can anyone give me some
quick pointers or point me to a good c strings tutorial? Below is a
program I am working on.

Kind Regards,
Anthony Irwin

#include <stdio.h>

char teststring[40] = "test string now";
char teststring1[40];

int main() {
printf("teststring = %s", teststring);
strcpy(teststring1, teststring);
printf("\nteststring1 = %s", teststring1);

/* How can I assign a new value to teststring say
* the value "I changed string text" */

printf("\n\nI changed teststring to = %s\n", teststring);

return 0;
}

推荐答案

2005年8月11日星期四09:54:28 +1,000,Anthony Irwin写道:
On Thu, 11 Aug 2005 09:54:28 +1000, Anthony Irwin wrote:


我是c的新手,我遇到字符串问题,任何人都可以给我一些快速指示或指向我好的C弦教程?以下是我正在制作的计划。

亲切的问候,
Anthony Irwin

#include< stdio.h>

char teststring [40] =" test string now" ;;
char teststring1 [40];

int main(){
printf(" teststring = %s",teststring);
strcpy(teststring1,teststring);
printf(" \\\
teststring1 =%s",teststring1);

/ *我该怎么办?为teststring指定一个新值说
*值我改变了字符串文本 * /
/ *你的意思是?再次调用strcpy()! * /

strcpy(teststring,我更改了字符串文本);
printf(" \ n\\\
II将teststring更改为=%s \ n",teststring) ;

返回0;
}
Hi,

I am new to c and am having trouble with strings can anyone give me some
quick pointers or point me to a good c strings tutorial? Below is a
program I am working on.

Kind Regards,
Anthony Irwin

#include <stdio.h>

char teststring[40] = "test string now";
char teststring1[40];

int main() {
printf("teststring = %s", teststring);
strcpy(teststring1, teststring);
printf("\nteststring1 = %s", teststring1);

/* How can I assign a new value to teststring say
* the value "I changed string text" */ /* What you mean? just call strcpy() again! */
strcpy(teststring, "I changed string text");
printf("\n\nI changed teststring to = %s\n", teststring);

return 0;
}








Anthony Irwin写道:

Anthony Irwin wrote:


我是c的新手,我遇到字符串问题,任何人都可以给我一些快速指针,或者指向一个好的c字符串教程?以下是我正在制作的计划。

亲切的问候,
Anthony Irwin

#include< stdio.h>

char teststring [40] =" test string now" ;;
char teststring1 [40];

int main(){
printf(" teststring = %s",teststring);
strcpy(teststring1,teststring);
printf(" \\\
teststring1 =%s",teststring1);

/ *我该怎么办?为teststring指定一个新值说
*值我改变了字符串文本 * /

printf(" \ n\\\
II将teststring更改为=%s \ n",teststring);

返回0;
}
Hi,

I am new to c and am having trouble with strings can anyone give me some
quick pointers or point me to a good c strings tutorial? Below is a
program I am working on.

Kind Regards,
Anthony Irwin

#include <stdio.h>

char teststring[40] = "test string now";
char teststring1[40];

int main() {
printf("teststring = %s", teststring);
strcpy(teststring1, teststring);
printf("\nteststring1 = %s", teststring1);

/* How can I assign a new value to teststring say
* the value "I changed string text" */

printf("\n\nI changed teststring to = %s\n", teststring);

return 0;
}




strncpy(teststring,我更改了字符串文本,sizeof teststring-1);





sprintf(teststring,"%。* s",sizeof teststring-1," I changed string

text");


转换说明符%。Ns表示不应写入不超过N

的字符;例如,我们可以写出

%。39s。使用*而不是常量意味着在

中指定了printf()的一个参数(在本例中,通过表达式

sizeof teststring-1)。


表达式sizeof teststring-1给出了目的地的大小

缓冲区,少了0个终结符的一个字符,因此在这两种情况下,没有

将超过39个字符写入teststring。 />



strncpy(teststring, "I changed string text", sizeof teststring-1);

or

sprintf(teststring, "%.*s", sizeof teststring-1, "I changed string
text");

The conversion specifier "%.Ns" indicates that no more than N
characters should be written; for example, we could have written
"%.39s". Using * instead of a constant means that N is specified in
one of the arguments to printf() (in this case, by the expression
sizeof teststring-1).

The expression "sizeof teststring-1" gives the size of the destination
buffer, less one character for a 0 terminator, so in both cases, no
more than 39 characters are being written to teststring.


感谢回复人员。出于某种原因,我认为你必须使用带有strcpy的

变量。


亲切的问候,

Anthony Irwin

Thanks for the reply guys. For some reason I thought you had to use
variables with strcpy.

Kind Regards,
Anthony Irwin


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

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