Strings

本文介绍了C语言中字符串的定义、格式化输出、长度计算、比较和连接等基本操作。通过示例代码展示了如何使用指针和字符数组定义字符串,并使用`printf`进行格式化打印,利用`strlen`计算字符串长度,`strncmp`进行字符串比较,以及`strncat`进行字符串连接。此外,还提供了练习题目,要求读者用不同方式定义两个字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Strings 字符串


Defining strings   定义字符串

Strings in C are actually arrays of characters(字符数组). Although using pointers(指针) in C is an advanced(高级的) subject, fully explained(详细说明 later on, we will use pointers to a character array to define simple strings, in the following manner(用以下方式):

char * name = "John Smith";

This method creates a string which we can only use for reading. If we wish to define a string which can be manipulated(操作), we will need to define it as a local(本地的) character array:

char name[] = "John Smith";

This notation(这种表示方法) is different because it allocates(分配了) an array variable so we can manipulate it. The empty brackets notation [] tells the compiler to calculate(计算) the size of the array automatically(自动地). This is in fact the same as allocating it explicitly(明确地), adding one to the length of the string:

char name[] = "John Smith";
/* is the same as */
char name[11] = "John Smith";

The reason that we need to add one, although the string John Smith is exactly 10 characters long, is for the string termination(终止字符: a special character (equal to 0) which indicates(表示) the end of the string. The end of the string is marked because the program does not know the length of the string - only the compiler knows it according to the code.


String formatting with printf  使用printf格式化字符串

We can use the printf command to format a string together with other strings, in the following manner:

char * name = "John Smith";
int age = 27;

/* prints out 'John Smith is 27 years old.' */
printf("%s is %d years old.\n", name, age);

Notice that when printing strings, we must add a newline (\n) character so that our next printf statement will print in a new line.


String Length  字符串长度

The function 'strlen' returns the length of the string which has to be passed as an argument:

char * name = "Nikhil";
printf("%d\n",strlen(name));


String comparison  字符串比较

The function strncmp compares between two strings, returning the number 0 if they are equal, or a different number if they are different. The arguments are the two strings to be compared, and the maximum comparison length. There is also an unsafe version(不安全的版本 of this function called strcmp, but it is not recommended(不建议) to use it. For example:

char * name = "John";

if (strncmp(name, "John", 4) == 0) {
    printf("Hello, John!\n");
} else {
    printf("You are not John. Go away.\n");
}


String Concatenation   字符串连接

The function 'strncat' appends(附加) first n characters of src string to the destination string(目标字符串) where n is min(n,length(src)); The arguments passed are destination string, source string(源字符串), and n - maximum number of characters to be appended. For Example:

char dest[20]="Hello";
char src[20]="World";
strncat(dest,src,3);
printf("%s\n",dest);
strncat(dest,src,20);
printf("%s\n",dest);




Exercise

Define the string first_name with the value John using the pointer notation, and define the string last_name with the value Doe using the local array notation.

 Start Exercise

原:

#include <stdio.h>
#include <string.h>
int main() {
  /* define first_name */
  /* define last_name */
  char name[100];

  last_name[0] = 'B';
  sprintf(name, "%s %s", first_name, last_name);
  if (strncmp(name, "John Boe", 100) == 0) {
      printf("Done!\n");
  }
  name[0]='\0';
  strncat(name,first_name,4);
  strncat(name,last_name,20);
  printf("%s\n",name);
  return 0;
}

 

改:

#include <stdio.h>
#include <string.h>
int main() {
  /* define first_name */
  /* define last_name */
  char *first_name = "John";
  char last_name[4] = "Doe";
  char name[100];

  last_name[0] = 'B';
  sprintf(name, "%s %s", first_name, last_name);
  if (strncmp(name, "John Boe", 100) == 0) {
      printf("Done!\n");
  }
  name[0]='\0';
  strncat(name,first_name,4);
  strncat(name,last_name,20);
  printf("%s\n",name);
  return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yangbocsu

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值