字符串数组和字符串指针数组_字符串和字符数组

字符串数组和字符串指针数组

String is a sequence of characters that is treated as a single data item and terminated by null character '\0'. Remember that C language does not support strings as a data type. A string is actually one-dimensional array of characters in C language. These are often used to create meaningful and readable programs.

字符串是一个字符序列,被视为单个数据项,并以空字符'\0'结尾。 请记住,C语言不支持将字符串作为数据类型。 字符串实际上是C语言中的一维字符数组。 这些通常用于创建有意义的可读程序。

For example: The string "hello world" contains 12 characters including '\0' character which is automatically added by the compiler at the end of the string.

例如:字符串“ hello world”包含12个字符,其中包括'\0'字符,该字符由编译器自动添加到字符串的末尾。

声明和初始化字符串变量 (Declaring and Initializing a string variables)

There are different ways to initialize a character array variable.

有多种初始化字符数组变量的方法。

char name[13] = "StudyTonight";       // valid character array initialization

char name[10] = {'L','e','s','s','o','n','s','\0'};     // valid initialization

Remember that when you initialize a character array by listing all of its characters separately then you must supply the '\0' character explicitly.

请记住,通过分别列出所有字符来初始化字符数组时,必须显式提供'\0'字符。

Some examples of illegal initialization of character array are,

非法初始化字符数组的一些示例是,

char ch[3] = "hell";    // Illegal

char str[4];
str = "hell";   // Illegal

字符串输入和输出 (String Input and Output)

Input function scanf() can be used with %s format specifier to read a string input from the terminal. But there is one problem with scanf() function, it terminates its input on the first white space it encounters. Therefore if you try to read an input string "Hello World" using scanf() function, it will only read Hello and terminate after encountering white spaces.

输入函数scanf()可以与%s格式说明符一起使用,以从终端读取字符串输入。 但是scanf()函数存在一个问题,它在遇到的第一个空白处终止输入。 因此,如果您尝试使用scanf()函数读取输入字符串“ Hello World”,它将仅读取Hello并在遇到空格后终止。

However, C supports a format specification known as the edit set conversion code %[..] that can be used to read a line containing a variety of characters, including white spaces.

但是,C支持一种称为编辑集转换代码%[..]的格式规范,该格式规范可用于读取包含各种字符(包括空格)的行。

#include<stdio.h>
#include<string.h>

void main()
{
    char str[20];
    printf("Enter a string");
    scanf("%[^\n]", &str);  //scanning the whole string, including the white spaces
    printf("%s", str);
}

Another method to read character string with white spaces from terminal is by using the gets() function.

从终端读取带有空格的字符串的另一种方法是使用gets()函数。

char text[20];
gets(text);
printf("%s", text);

字符串处理函数 (String Handling Functions)

C language supports a large number of string handling functions that can be used to carry out many of the string manipulations. These functions are packaged in string.h library. Hence, you must include string.h header file in your programs to use these functions.

C语言支持大量的字符串处理功能,这些功能可用于执行许多字符串操作。 这些函数打包在string.h库中。 因此,您必须在程序中包括string.h头文件才能使用这些功能。

The following are the most commonly used string handling functions.

以下是最常用的字符串处理函数。

MethodDescription
strcat()It is used to concatenate(combine) two strings
strlen()It is used to show length of a string
strrev()It is used to show reverse of a string
strcpy()Copies one string into another
strcmp()It is used to compare two string
方法 描述
strcat() 它用于连接(组合)两个字符串
strlen() 用于显示字符串的长度
strrev() 用于显示字符串的反向
strcpy() 将一个字符串复制到另一个
strcmp() 用于比较两个字符串
strcat()函数 (strcat() function)
strcat("hello", "world");
strcat() function will add the string strcat()函数会将字符串 "world" to “ world”添加到 "hello" i.e it will ouput helloworld. “ hello” ,即将输出helloworld。
strlen()函数 (strlen() function)

strlen() function will return the length of the string passed to it.

strlen()函数将返回传递给它的字符串的长度。

int j; 
j = strlen("studytonight");
printf("%d",j);

12

12

strcmp()函数 (strcmp() function)

strcmp() function will return the ASCII difference between first unmatching character of two strings.

strcmp()函数将返回两个字符串的第一个不匹配字符之间的ASCII差异。

int j; 
j = strcmp("study", "tonight");
printf("%d",j);

-1

-1

strcpy()函数 (strcpy() function)

It copies the second string argument to the first string argument.

它将第二个字符串参数复制到第一个字符串参数。

#include<stdio.h>
#include<string.h>

int main()
{
    char s1[50];
    char s2[50];

    strcpy(s1, "StudyTonight");     //copies "studytonight" to string s1
    strcpy(s2, s1);     //copies string s1 to string s2

    printf("%s\n", s2);
    
    return(0);
}

StudyTonight

今晚学习

strrev()函数 (strrev() function)

It is used to reverse the given string expression.

它用于反转给定的字符串表达式。

#include<stdio.h>

int main()
{ 
    char s1[50]; 
  
    printf("Enter your string: "); 
    gets(s1);  
    printf("\nYour reverse string is: %s",strrev(s1)); 
    return(0); 
}

Enter your string: studytonight Your reverse string is: thginotyduts

输入您的字符串:studytonight您的反向字符串是:thginotyduts

翻译自: https://www.studytonight.com/c/string-and-character-array.php

字符串数组和字符串指针数组

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值