Monday, 22 June 2020

String Handling C Language


String

In C programming, a string is a sequence of one or more than any characters terminated with a null character. In C language you can only save string in one dimensional character array , no separate data type for string like java or c#.
Declare a string
     char str = "GIRFA";
G
I
R
F
A
\0

Compiler will create an array of 6 element one extra cell for null character which is used to detect end of string.

Input/Output


char str[10];
     printf("Enter string>>");
     scanf("%s", str);

     for (i = 1; i<str[i] != '\0'; i++)
          printf("%c", str[i]);
This example shows you input a string without space and process each character.
     char str[10];
     printf("Enter string>>");
     gets(str);
     puts(str);
This example shows how to input space and print using library function.
C language compiler has reach variety of string handling functions string.h library file. Some popular function examples are given below.

1 strcpy

Copy a string into other
Declaration:
   char *strcpy(char *dest, const char *src);
   char far * _fstrcpy(char far *dest, const char far *src);

 Remarks:
Copies string src to dest, stopping after the terminating null character has
been moved.
int main(void)
 {
    char string[10];
    char *str1 = "abcdefghi";

    strcpy(string, str1);
    printf("%s\n", string);
    return 0;
 }

2. Strlen

Calculates length of a string

 Declaration:
   size_t strlen(const char *s);
   size_t far _fstrlen(const char far *s);

 Remarks:
strlen calculates the length of s.

 Return Value:
Returns the number of characters in s, not counting the terminating null
character.
int main(void)
{
   char *string = "Borland International";

   printf("%d\n", strlen(string));
   return 0;
}

3. strcat

Appends one string to another

 Declaration:
   char *strcat(char *dest, const char *src);
   char far * far _fstrcat(char far *dest, const char far *src);

 Remarks:
strcat appends a copy of src to the end of dest. The length of the resulting
string is strlen(dest) + strlen(src).


int main(void)
{
   char destination[25];
   char *blank = " ", *c = "C++", *turbo = "Turbo";

   strcpy(destination, turbo);
   strcat(destination, blank);
   strcat(destination, c);

   printf("%s\n", destination);
   return 0;
}

4.strcmp

strcmp compare two strings

 Declaration:
   int strcmp(const char *s1, const char*s2);


 Remarks:
 strcmp performs an unsigned comparison of s1 to s2. _fstrcmp is the far
version.

Return Value:
These routines return an int value that is
   <  0  if s1 <  s2
   == 0  if s1 == s2
   >  0  if s1 >  s2



int main(void)
 {
    char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
    int ptr;

    ptr = strcmp(buf2, buf1);
    if (ptr > 0)
       printf("buffer 2 is greater than buffer 1\n");
    else
       printf("buffer 2 is less than buffer 1\n");

    ptr = strcmp(buf2, buf3);
    if (ptr > 0)
       printf("buffer 2 is greater than buffer 3\n");
    else
       printf("buffer 2 is less than buffer 3\n");

    return 0;
 }


5. strrev


 Reverses all characters in s (except for the terminating null)

 Declaration:
   char *strrev(char *s);
   char far * far _fstrrev(char far *s);

 Remarks:
strrev and _fstrrev changes all characters in a string to reverse order,
except the terminating null character.

For example, it would change
  string\0
to
  gnirts\0.)

 Return Value:
strrev returns a pointer to the reversed string.




int main(void)
{
   char *string = "Borland International";

   printf("string prior to strlwr: %s\n", string);
   strlwr(string);
   printf("string after strlwr:    %s\n", string);
   return 0;
}




No comments:

Post a Comment