c语言字符串中查找字符串_C中的字符串–第1部分

c语言字符串中查找字符串

Till now I told you about the arrays in C language. I have also explained the close relation between arrays and pointers. Now lets move this journey to one step forward. Today I will tell you about the strings in C language. In this tutorial I will cover all the basics related to strings.

String in C

Earlier we used integer arrays to store group of integer elements. Similarly in C language we use strings to store group of characters. They are used to manipulate text such as words and sentences. Remember strings are similar to arrays but they are not same. Sometimes strings are also called character arrays.

A string always ends with a null character i.e. ‘’. Basically this character is used by the compiler to judge the length of the string. Remember ‘’ and ‘0’ are different characters. Both have different ASCII value and have different meaning.

Declaration of String

String can be declared in the same way we declare an array. We just need to use char data type followed by string name and its size. See below example.

char a[10];

Initialization of String

char name[]={‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘’};

or

char name[]=”Hello”;

Obviously the second method is far better than the first one. But consider carefully I haven’t added ‘’ at the end of “Hello”. Well I don’t need to worry about that in the second method because compiler will automatically add null character at the end of it.

String in C (Memory Representation) – Image Source

Printing the String Elements

There are many ways to print the string elements on the screen. I will tell you the programs which are used very commonly.

#include<stdio.h>
 
int main()
{
 char name[]="TheCrazyProgrammer";
 int x=0;
 
 while(x<18)
 {
  printf("%c",name[x]);
  x++;
 }
 
 return 0;
 
}

Output

TheCrazyProgrammer

The above program is self-explanatory. I have initialized one string and I have printed the character elements by using while loop and subscript. Its similar to the program which I have used earlier to print the array elements. Now lets move on to a better version of this.


Output of the program is same but the program is different. In this program I have omitted the boundation of writing the length of the string. It will print all the elements until it will get null character ‘’. So it’s a better way of accessing string elements. Now lets make one program by using pointers.

#include <stdio.h>
 
int main( ) 
{  
 char name[]="TheCrazyProgrammer";
 char *p;  
 p=name; 
 
 while(*p!='')  
 {   
  printf("%c",*p);   
  p++; 
 }
 
 return 0;
}


This program is similar to the last one. But in this program I have used pointer p for accessing the string elements instead of using subscript. The statement p=name; will store the address of first character of string in the pointer p. Now in the while loop each character is accessed by incrementing the address stored in pointer p.

Strings is one of the most important topics in C programming, as all the text manipulation can be done only through strings. So I would strongly suggest you to read the basics of strings at least twice. So that you can easily understand the advance topics.

翻译自: https://www.thecrazyprogrammer.com/2015/02/string-in-c-part-1.html

c语言字符串中查找字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值