C - SPLessons

Chapter 25

String in C

String in C

As discussed in previous chapters, there is no string data type in C-language. Hence, the group of characters without data type forming a word, can be given individually as an array of characters using String in C .

A string can be defined as an array of characters which are ended using null character(\0).To read strings given by user, %s is used in scanf function.

Declaration of String in C

String declaration will be similar to array declaration. But here, strings are of char type.

Eg: char i[5];

Initialization of String in C

C strings can be initialized in various ways.

Eg:

 char i[]="xyz";
     OR
char i[5]="xyz";
     OR
char i[]={'x','y','z','\0'};
     OR
char i[4]={'x','y','z','\0'};

#include <stdio.h>
int main(){
    char name[20];
    printf("Enter name: ");
    scanf("%s",name);
    printf("Your name is %s.",name);
    return 0;
}

String Functions Description
strrev() Reverses teh given string
strset() Sets all character in a string to given character
strnset() Sets the portion of characters in a string to given character
strtok() Tokenizing given string using delimiter
strncpy() Copies given number of characters of one string to another
strlen() Gives the length of string
strdup() Duplicates the string
strlwr() Converts the string to lower case
strupr() Converts the string to upper case
strcat() Concatenates the two string values
strcmp() Compares the two strings
strcmpi() Compares the two strings but it is not case sensitive
strchr() Pointer points to first character in string
strrchr() Pointer points to last character in string
strstr() Pointer points to first character of second string in first
strrstr() Pointer points to first character of second string in first string

Reading text of line manually is tedious and hence gets() and puts() functions are used in C language to read and display string respectively.

#include<stdio.h>
#include<conio.h>
int main(){
    char name[30];
    printf("Enter name: ");
    gets(name);     //Function to read string from user.
    printf("Name: ");
    puts(name);    //Function to display string.
    return 0;
}
Enter name: SPLessons Tutorial
Name: SPLessons Tutorial

Strings with Functions

Functions in C can be defined as a partitioned blocks of code in a program, which can be called any number of times during single execution of a program.

String can be passed to a function similar to arrays as, string is also an array.

#include <stdio.h>
void Display(char p[]);
int main(){
    char s[50];
    printf("Enter any string: ");
    gets(s);             
    Display(s);     // Passing string c to function.    
    return 0;
}
void Display(char p[]){
    printf("String Output: ");
    puts(p);
}

Output:

Enter any string: SPLessons Tutorial
String Output: SPLessons Tutorial

Summary

  • String is group of characters.
  • In Initialization of the string, if the specific number of character are not initialized then rest of characters will be initialized with NULL(/0).

  • Strings are always enclosed in double quotes(”  “).
  • String variable str accepts only one word because if white space is found, the scanf() function terminates the program. So, to prevent use gets() function.