Thursday, November 12, 2020

Strings

The string can be defined as the one-dimensional array of characters terminated by a null ('\0'). The character array or the string is used to manipulate text such as word or sentences. Each character in
the array occupies one byte of memory, and the last character must always be \0. The termination character ('\0') is important in a string since it is the only way to identify where the string ends.

Declaration of strings:

Declaring a string is as simple as declaring a one dimensional array. Below is the basic syntax for declaring a string.

char   str_name[size];

In the above syntax str_name is any name given to the string variable and size is used define the length of the string, i.e the number of characters strings will store.

Initializing a String:

 A string can be initialized in different ways. 

 char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

or

char greeting[] = "Hello";

The C compiler automatically places the '\0' at the end of the string when it initializes the array.
Ex: 
#include<stdio.h>  
#include <string.h>    
void main(){    
  char ch[11]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};    
   char ch2[11]="javatpoint";    
   printf("Char Array Value is: %s\n", ch);    
   printf("String Literal Value is: %s\n", ch2);    
}    

The C language does not provide an inbuilt data type for strings but it has an access specifier “%s” which can be used to directly print and read strings.

Functions from ctype.h

The ctype.h header file of the C Standard Library declares several functions that are useful for testing and mapping characters.

All the functions accepts int as a parameter, whose value must be EOF or representable as an unsigned char.

All the functions return non-zero (true) if the argument c satisfies the condition described, and zero(false) if not.

     isdigit() - checks numeric character


      isalnum() - checks whether the argument passed is an alphanumeric character (alphabet or number) or not.

      islower() - checks lowercase alphabet

     isupper() - checks uppercase alphabet


     tolower() - converts alphabet to lowercase


     toupper() - converts alphabet to uppercase


     isspace() - check white-space character

     isalpha() - checks whether a character is an alphabet or not

Functions from string.h

The string.h header defines one variable type, one macro, and various functions for manipulating
arrays of characters.

There are some common inbuilt functions to manipulation on string in string.h file.

1. strlen – finds string length

2. strcpy – used for string copy

3. strcmp – used for string compare

4. strupr – convert string to upper case

5. strlwr – convert string to lower case

6. strcat – used for string concatenate

1. strlen()
strlen() is used to find length of a string. strlen() function returns the length of a string. It returns integer value.

Example: 

char *str = "Learn C";

int strLength;

 strLength = strlen(str);    //strLength contains thelength of the string i.e. 7

2. strcpy()

strcpy() function is used to copy one string to another. strcpy() function accepts 2 parameters. First parameter is the destination string i.e. the string variable used to copy the string into. Second parameter is the source string i.e. the string variable or string value that needs to be copied to destination
string variable. 

Syntax:

strcpy(Destination_String,Source_String);

3. strcat()

strcat() is used to concatenate two strings.

The Destination_String should be a variable and Source_String can either be a string constant or a variable.

Syntax:

strcat(Destination_String, Source_String);

4. strcmp()

strcmp() function is use two compare two strings. strcmp() function does a case sensitive comparison between two strings. The Destination_String and Source_String can either be a string constant or a variable.

0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top