Code Scrappers

Strings

The string in C is actually a one-dimensional array of characters which is terminated by a null character '\0'. A null-terminated string contains the characters that comprise the string which is followed by a null.

Any valid C variable name is a string variable and is always declared as an array.

The generalised format of the declaration of a string variable is as follows :

Eg:
    char class[10];

    char name[30];

When a character string is assigned by the compiler to a character array, it automatically supplies a null character (‘\0’) at the end of the string. Thus, the size should be equal to the maximum number of characters in the string plus one. C allows a character array to be initialized in either of the following two forms.

static char country[9] = “INDIA”;

static char country[9] = {‘I, ‘N’, ‘D’, ‘I’, ‘A’,‘\0’};

Reading Words

The input function scanf can be used with %s format specification to read in a string of characters.

Eg:
    char name[15];

    scanf(“%s”, name);

Program:

 #include<stdio.h>

 void main()

{

  char word1[50],word2[50],word3[50],word4[50];

  printf("Enter the words:\n");

  scanf("%s",word1);

  scanf("%s",word2);

  scanf("%s", word3);

  scanf("%s",word4);

  printf("\n");

  printf("Word one = %s \nword two = %s \n",word1, word2);

  printf("word three = %s \nword four = %s \n",word3, word4);

}

OUTPUT:
Enter the words:
This
is
C
code
Word one = This
Word two =is
Word three = C
Word four = Code

NOTE: Scanf function terminates its input on the first white space it encounters.

Reading a Line of Text

The input function scanf can be used with %s format specification to read in a string of characters.

Eg: char day[15];
scanf(“%s”, day);

String handling functions -string.h

strcat

Appends a string

strchr

Finds first occurrence of a given character

strcmp

Compares two strings

strcmpi

Compares two strings, non-case sensitive

strcpy

Copies one string to another

strlen

Finds length of a string

strlwr

Converts a string to lowercase

strncat

Appends n characters of string

strncmp

Compares n characters of two strings

strncpy

Copies n characters of one string to another

strnset

It is used to set n characters of string to a given character

strrchr

Finds last occurrence of given character in string

strrev

Reverses string

strset

It is used to set all characters of string to a given character

strspn

It is used to find first substring from given character set in string

strupr

Converts string to uppercase

Character handling functions- ctype.h

The character handling functions that are defined in ctype.h are as follows:

isalnum

Tests for alphanumeric character

isalpha

Tests for alphabetic character

isascii

Tests for ASCII character

iscntrl

Tests for control character

isdigit

Tests for 0 to 9

isgraph

Tests for printable character

islower

Tests for lowercase

isprint

Tests for printable character

ispunct

Tests for punctuation character

isspace

Tests for space character

isupper

Tests for uppercase character

isxdigit

Tests for hexadecimal

toascii

Converts character to ascii code

tolower

Converts character to lowercase

toupper

Converts character to uppercase

C program to convert a string array to uppercase a character at a time using toupper()

 #include<stdio.h>

#include <ctype.h>

 void main()

 {

    char name[100];

    int i;

    printf("Enter the name in lowercase\n");

    scanf( "%s", name );

    for( i = 0; name[i] != 0; i++ )

    name[i] = toupper( name[i] );

    printf("The name is uppercase is %s", name );

}

OUTPUT:
Enter in a name in lowercase
adrian
The name in uppercase is ADRIAN

<< Back Next >>