AsegGasiaBlog

C Programming Part 9

String

Group of characters can be stored in a character array. String in C language is an array of characters that is terminated by \0 (null character)

Declaration :

There are two ways to declare string in c language.

  • By char array
  • By string literal

string by char array

Syntax :

char ch[]={'h','e','l','l','o','\0'};

Note :

  • Declaring string size is not mandatory.

string by string literal

Syntax :

char ch[]="hello";

Note :

  • ‘\0’ is not necessary. C inserts the null character automatically.

  • Important Points :

  • The '%s' is used to print string in c language.

Example of string :


#include <stdio.h>

int main(void)
{
    char ch[11] = {'h', 'e', 'l', 'l', 'o', '\0'};
    char ch2[11] = "hello";
    char c = 'h';

    printf("Character value is : %c\n",c);
    printf("\nChar Array Value is : %s\n", ch);
    printf("\nString Literal Value is : %s\n", ch2);

    return 0;
}

Output :

Character value is : h
Char Array Value is : hello
String Literal Value is : hello
    

Example of gets and puts function :


#include <stdio.h>

int main(void)
{
    char name[25] ;

    printf ( "Enter name : " ) ;
    gets ( name ) ;//for input

    printf("\nYour Name is : ");
    puts ( name ) ;//for output

    return 0;
}

Output :

Enter name : hello
Your Name is : hello

String Functions
1. strlen
2. strcpy
3. strcat
4. strcmp

1. strlen

The strlen() function returns the length of the given string. It doesn't count null character '\0'.

Syntax :

strlen(string_name)

Example :


#include <stdio.h>

int main(void)
{
    char ch[20]={'h','e','l','l','o','\0'};
    printf("Length of string is: %d",strlen(ch));
    return 0;
}

Output :

Length of string is: 5

2. strcpy

The strcpy(destination, source) function copies the source string in destination.

Syntax :

strcpy(destination, source)

Example :


#include <stdio.h>

int main(void)
{
    char source[20]={'h','e','l','l','o','\0'};
    char destination[20];

    strcpy(destination,source);

    printf("Value of second string is : %s",destination);

    return 0;
}

Output :

Value of second string is : hello

3. strcat
The strcat(first_string, second_string) function concatenates two strings and result is returned to first_string.

Syntax :

strcat(first_string, second_string)

Example :


#include <stdio.h>

int main(void)
{
    char firststring[10]={'h','e','l','l','o',' ','\0'};
    char secondstring[10]={'w','o','r','l','d','\0'};

    strcat(firststring,secondstring);

    printf("Value of first string is : %s",firststring);
}

Output :

Value of first string is : hello world

4. strcmp

The strcmp(first_string, second_string) function compares two strings.

Syntax :

strcmp(first_string, second_string)

Example :


#include <stdio.h>

int main(void)
{
    char firststring[20],secondstring[20];

    printf("Enter first string : ");
    gets(firststring);//reads string from console

    printf("\nEnter second string : ");
    gets(secondstring);

    if(strcmp(firststring,secondstring)==0)
    printf("\nStrings are equal");

    else
    printf("\nStrings are not equal");

}




Popular Posts