Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Thursday, 9 January 2014

String Manipulations In C Programming Using Library Functions

String Manipulations In C Programming Using Library Functions

Strings are often needed to be manipulated by programmer according to the need of a problem. All string manipulation can be done manually by the programmer but, this makes programming complex and large. To solve this, the C supports a large number of string handling functions.
There are numerous functions defined in "string.h" header file. Few commonly used string handling functions are discussed below:
Function
Work of Function
strlen()
Calculates the length of string
strcpy()
Copies a string to another string
strcat()
Concatenates(joins) two strings
strcmp()
Compares two string
strlwr()
Converts string to lowercase
strupr()
Converts string to uppercase
Strings handling functions are defined under "string.h" header file, i.e, you have to include the code below to run string handling functions.


#include <string.h>





strlen()

In C, strlen() function calculates the length of string. It is defined under "string.h" header file.
It takes only one argument, i.e, string name.

Syntax of strlen()

temp_variable = strlen(string_name);
Function strlen() returns the value of type integer.
calculation of string using strlen() library function.

Example of strlen()

#include <stdio.h>
#include <string.h>
int main(){
    char a[20]="Program";
    char b[20]={'P','r','o','g','r','a','m','\0'};
    char c[20];
    printf("Enter string: ");
    gets(c);  
    printf("Length of string a=%d \n",strlen(a));
    //calculates the length of string before null charcter.
    printf("Length of string b=%d \n",strlen(b));   
    printf("Length of string c=%d \n",strlen(c));
    return 0;
}
Output
Enter string: String
Length of string a=7
Length of string b=7
Length of string c=6


strcpy()

Function strcpy() copies the content of one string to the content of another string. It is defined under "string.h" header file.
It takes two arguments.

Syntax of strcpy()

strcpy(destination,source);
Here, source and destination are both the name of the string. This statement, copies the content of string source to the content of string destination.

Example of strcpy()

#include <stdio.h>
#include <string.h>
int main(){
    char a[10],b[10];
    printf("Enter string: ");
    gets(a);
    strcpy(b,a);   //Content of string a is copied to string b.
    printf("Copied string: ");
    puts(b);
    return 0;
}
Output
Enter string: Programming Tutorial
Copied string: Programming Tutorial

strcat()

In C programming, strcat() concatenates(joins) two strings.
It takes two arguments, i.e, two strings and resultant string is stored in the first string specified in the argument.
Function strcat() is defined under "string.h" header file.

Syntax of strcat()

strcat(first_string,second_string);

Example of strcat()

#include <stdio.h>
#include <string.h>
int main(){
    char str1[]="This is ", str2[]="programiz.com";
    strcat(str1,str2);   //concatenates str1 and str2 and resultant string is stored in str1.
    puts(str1);    
    puts(str2); 
    return 0;
}
Output
This is programiz.com
programiz.com


strcmp()

In C, strcmp() compares two string and returns value 0, if the two strings are equal. It is defined under "string.h" header file.
Function strcmp() takes two arguments, i.e, name of two string to compare.

Syntax of strcmp()

temp_varaible=strcmp(string1,string2);

Example of strcmp()

#include <stdio.h>
#include <string.h>
int main(){
  char str1[30],str2[30];
  printf("Enter first string: ");
  gets(str1);
  printf("Enter second string: ");
  gets(str2);
  if(strcmp(str1,str2)==0)
      printf("Both strings are equal");
  else
      printf("Strings are unequal");
  return 0;
}
Output
Enter first string: Apple     
Enter second string: Apple
Both strings are equal.
If two strings are not equal, strcmp() returns positive value if ASCII value of first mismatching element of first string is greater than that of second string and negative value if ASCII value of first mismatching element of first string is less than that of second string. For example:
char st1r[]="and",str2[]="cat";
temp=strcmp(str1,str2); 
Since, ASCII value of 'a' is less than that of 'c', variable temp will be negative.


strlwr()

In C programming, strlwr() function converts all the uppercase characters in that string to lowercase characters.
The resultant from strlwr() is stored in the same string.

Syntax of strlwr()

strlwr(string_name);

Example of strlwr()

 
 
#include <stdio.h>
#include <string.h>
int main(){
    char str1[]="LOWer Case";
    puts(strlwr(str1));   //converts to lowercase and displays it.
    return 0;
}
Output
lower case
Function strlwr() leaves the lowercase characters as it is and converts uppercase characters to lowercase.

strupr()

In C programming, strupr() function converts all the lowercase characters in that string to uppercase characters.
The resultant from strupr() is stored in the same string.

Syntax of strupr()

strupr(string_name);

Example of strupr()

#include <stdio.h>
#include <string.h>
int main(){
    char str1[]="UppEr Case";
    puts(strupr(str1));   //Converts to uppercase and displays it.
    return 0;
}
Output
UPPER CASE

Function strupr() leaves the uppercase characters as it is and converts lowercase characters to uppercase.

No comments:

Post a Comment