Home » C and C++, Tech Corner

Strings(Character Arays)

23 August 2009 No Comments Posted By:Dileep
  • A string is nothing but an array of characters.
  • Two strings can be compared, concatenated or copied. We can convert upper case letters into the lower case letters and vice-versa. Strings can be reversed. We can also check for the substrings present in the given string.
  • String is a one-dimensional array of characters.
  • Each one-dimensional character array(string) ends with a null character. So an n-character array contains (n+1) array elements.
  • C language provides many string handling functions which are defined in the header file .
  • Like a numeric array, character arrays must be declared before they appear in a C program.
  • There is no change in the syntaxof declaring stings except the change in the data type(char).

Reading Strings

  • scanf() with the %s format specification
  • gets() function
  • getchar() funcation



Writing Strings

  • printf() with the %s format specification
  • puts() function
  • putchar() function
  • A two-dimensional character array is an array of one-dimensional character arrays.
  • This means that a two-dimensional character array consists of strings as its individual elements.
  • Like two-dimensional numeric arrays, two-dimensional character arrays must be declared before they are used in the program and should also be initialized.

String Handling Functions

  • C supports a number of string handling functions.
  • These functions are defined in the header file <string.h>.

strcmp() function – This function compares two strings character by character(ASCII comparison) and returns one of the three values {-1,0,1}.
Syntax – strcmp(string1,string2);

strcpy() function – This function is used to copy one string to the other.
Syntax – strcpy(string1,string2);

strcat() function – This function is used to concatenate two strings. That is, it appends one string at the end of the specified string.
Syntax – strcat(string1,string2);

strlen() function – This returns the number of characters in the string(i.e. string lenght). Here, the string length does not include the NULL character.
Syntax – strlen(string);

strncmp() function – This compares the first n characters of two input strings.
Syntax – strncmp(string1,string2,n);

strncpy() function – This copies first n characters of the second string to the first string.
Syntax – strncpy(string1,string2,n);

strncat() function – This appends first n characters of the second string at the end of the first string.
Syntax – strncat(string1,string2,n);

strlwr() function – This function is used to convert any uppercase letters present in a string to its equivalent lowercase.
Syntax – strlwr(string);

strupr() function – It converts any lowercase letters that appear in the input string to the uppercase.
Syntax – strupr(string);

Implementation of String Functions

  • String Concatenation – String concatenation is a process of appending one string to the end of another string.
  1. Read two strings and call them as string1 and string2.
  2. Traverse the first string to its end.
  3. Attach the second string to the end of the first string.
  4. Set the last character of the resultant string to a null character(\0).

char *stringcat (char str1[], char str2[])
{
int i, j, pos;
for (i=0;str1[i]!= ‘\0′; i++)
{
;
}
pos=i;
for (i=pos; str1[i]!= ‘\0′; i++)
{
str1[i] = str2[j++];
}
str1[i] = ‘\0′;
return (str1);
}

  • String comparison – This is a process of comparing two strings to check whether they are equal or not.
  1. Read two strings and call them as string1 and string2.
  2. Find the length of each string.
  3. Set the counter to zero.
  4. Start comparing the first character of both strings and if they are equal then increment the counter and continue for the subsequent characters until the corresponding characters differ or till the end of the strings is reached.
  5. Return zero, if they are equal; 1 if the string1 is greater than the second; and -1 the string1 is less than the string2.

int stringcomp (char str1[], char str2[], strlenght1, strlength2)
{
int i = 0, flag = 0;
while ((i < strlength1) && (i < strlength2))
{
if (str1[i] == str2[i])
{
i++;
continue;
}
if (str1[i] < str2[i])
{
flag = -1;
break;
}
if (str1[i] > str2[i])
{
flag = 1;
break;
}
}
return (flag);
}

  • String length – This is a process of finding the number of characters present in a given string.
  1. Read a string.
  2. Set the counter to zero.
  3. Start traversing the string character-by-character and increment the counter everytime a new character is encountered.
  4. Repeat step 3, until all the characters are traversed in the string.

nt stringlen (char string[])
{
int i, length = 0;
for (i=0; string[i]!= ‘\0′;i++)
{
length = length + 1;
}
return (length);
}

<<Previous Post

Next Post>>

Related posts:

  1. Arrays
  2. Pointers
  3. Data Files
  4. Functions
  5. Constants and Variables

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>