strings

Strings

http://install4install.com

String is a collection of characters that forms a word.It is stored in one dimensional array and terminated by null(\0) character.The size of array is one more than the length of string as it required to store null character.The compiler itself places a null character at the end of array during its initialization

Syntax

char array_name[array_size] = {‘character 1′,’character 2′,…….,’\0′};

or

char array_name[] = ‘string_name';

Example

#include <stdio.h>
int main() 
{
char website[12]= {'L','e','t','s','l','e','a','r','n','C','S','\0'};//or char website[]="LetslearnCS";
printf("Name of the website is:\t");
printf("%s",website);
return 0;
}

Run

 

 Memory Representation of String

The memory representation of a string is given in Fig 1.The addresses of character is assumed for

Screen Shot 2014-07-11 at 12.31.36 PM

Fig 1 : Representation of a String in memory

 

String Functions

There are various predefined  functions in C that are used to perform various operations on strings.In order to use these function a header file string.h must be included in the program.The function definitions are placed in this file.We just call these functions through function call statements.

  • strlen()
  • strcpy()
  • strcat()
  • strcmp()
  • strsstr()
  • strchr()

strlen()

This function finds the length of string and gives answer in numeric form.

Syntax

strlen(string_name);

Example

#include <stdio.h>
#include<string.h>//header file for string functions

int main()
{
	char website_name[] ="LetsLearnCS";//string initialization
	int len;//len is of int type to store length of string
	len = strlen(website_name);//function call
	printf("The length of string is %d",len);
	return 0;
}

 

Run

TipIf a space is given in between the string,then that is also counted in length by strlen function.

 

strcpy()

strcpy function copies the content of one string into another string.

Syntax

strcpy(string_1,string_2);

It copies the content of string 2 into string 1.

Example

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

int main() 
{
char string1[] = "Two-Dimensional";
char string2[] = "Arrays";
printf("String 1 before strcpy() function :%s\n",string1);
printf("String 2 before strcpy() function: %s\n\n",string2);
strcpy(string1,string2);
printf("String 1 after strcpy() function: %s\n",string1);
printf("String 2 after strcpy()function: %s\n",string2);
return 0;
}

Run

 

 

strcat()

This function concatenates/combines two strings.

Syntax

strcat(string1,string2);

string2 is combined to end of string1.The second string remains as it is.

Example

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

int main() 
{
char string1[] = "Two-Dimensional";
char string2[] = "Arrays";
printf("String 1 before strcat() function :%s\n",string1);
printf("String 2 before strcat() function: %s\n\n",string2);
strcat(string1,string2);
printf("String 1 after strcat() function: %s\n",string1);
printf("String 2 after strcat()function: %s\n",string2);
return 0;
}

Run

 

strcmp()

This function returns 0 if string 1 and string 2 are same and 1 if they are not same.If the first or few initial characters of both the strings are same then it returns -1.

Syntax

strcmp(string1,string2)

Example

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

int main() 
{
char string1[] = "Arrays";
char string2[] = "Arrays";
int val;
val= strcmp(string1,string2);
printf("%d",val);
return 0;
}

Run

 

 


Short URL: http://tinyit.cc/8f4440
By Cusp2207 on July 11, 2014 | Computer Science | A comment?
Tags: