String Function in C

C String Functions

C String Functions

There are many important string functions defined in “string.h” library.

 

No. Function Description
1) strlen(string_name) returns the length of string name.
2) strcpy(destination, source) copies the contents of source string to destination string.
3) strcat(first_string, second_string) concats or joins first string with second string. The result of the string is stored in first string.
4) strcmp(first_string, second_string) compares the first string with second string. If both strings are same, it returns 0.
5) strrev(string) returns reverse string.
6) strlwr(string) returns string characters in lowercase.
7) strupr(string) returns string characters in uppercase.
C String Length: strlen() function
The strlen() function returns the length of the given string. It doesn’t count null character ‘\0’.
#include<stdio.h>
#include <string.h>
int main(){
char ch[20]={‘j’, ‘a’, ‘v’, ‘a’, ‘t’, ‘p’, ‘o’, ‘i’, ‘n’, ‘t’, ‘\0’};
   printf(“Length of string is: %d”,strlen(ch));
 return 0;
}
Output:
Length of string is: 10
C Copy String: strcpy()
The strcpy(destination, source) function copies the source string in destination.
#include<stdio.h>
#include <string.h>
int main(){
 char ch[20]={‘j’, ‘a’, ‘v’, ‘a’, ‘t’, ‘p’, ‘o’, ‘i’, ‘n’, ‘t’, ‘\0’};
   char ch2[20];
   strcpy(ch2,ch);
   printf(“Value of second string is: %s”,ch2);
 return 0;
}
Output:
Value of second string is: cloudandclear
C String Concatenation: strcat()
The strcat(first_string, second_string) function concatenates two strings and result is returned to first_string.
#include<stdio.h>
#include <string.h>
int main(){
  char ch[10]={‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};
   char ch2[10]={‘c’, ‘\0’};
   strcat(ch,ch2);
   printf(“Value of first string is: %s”,ch);
 return 0;
}
Output:
Value of first string is: helloc
C Compare String: strcmp()
The strcmp(first_string, second_string) function compares two string and returns 0 if both strings are equal.
Here, we are using gets() function which reads string from the console.
#include<stdio.h>
#include <string.h>
int main(){
  char str1[20],str2[20];
  printf(“Enter 1st string: “);
  gets(str1);//reads string from console
  printf(“Enter 2nd string: “);
  gets(str2);
  if(strcmp(str1,str2)==0)
      printf(“Strings are equal”);
  else
      printf(“Strings are not equal”);
 return 0;
}
Output:
Enter 1st string: hello
Enter 2nd string: hello
Strings are equal
C Reverse String: strrev()
The strrev(string) function returns reverse of the given string. Let’s see a simple example of strrev() function.
#include<stdio.h>
#include <string.h>
int main(){
  char str[20];
  printf(“Enter string: “);
  gets(str);//reads string from console
  printf(“String is: %s”,str);
  printf(“\nReverse String is: %s”,strrev(str));
 return 0;
}
Output:
Enter string: cloudandclear
String is: cloudandclear
Reverse String is: tnioptavaj
C String Lowercase: strlwr()
The strlwr(string) function returns string characters in lowercase. Let’s see a simple example of strlwr() function.
#include<stdio.h>
#include <string.h>
int main(){
  char str[20];
  printf(“Enter string: “);
  gets(str);//reads string from console
  printf(“String is: %s”,str);
  printf(“\nLower String is: %s”,strlwr(str));
 return 0;
}
Output:
Enter string: cloudandclear
String is: cloudandclear
Lower String is: cloudandclear
C String Uppercase: strupr()
The strupr(string) function returns string characters in uppercase. Let’s see a simple example of strupr() function.
#include<stdio.h>
#include <string.h>
int main(){
  char str[20];
  printf(“Enter string: “);
  gets(str);//reads string from console
  printf(“String is: %s”,str);
  printf(“\nUpper String is: %s”,strupr(str));
 return 0;
}
Output:
Enter string: cloudandclear
String is: cloudandclear
Upper String is: CLOUDANDCLEAR
C String strstr()
The strstr() function returns pointer to the first occurrence of the matched string in the given string. It is used to return substring from first match till the last character.
Syntax:
char *strstr(const char *string, const char *match)
String strstr() parameters
string: It represents the full string from where substring will be searched.
match: It represents the substring to be searched in the full string.
String strstr() example
#include<stdio.h>
#include <string.h>
int main(){
  char str[100]=”this is cloudandclear with c and java”;
  char *sub;
  sub=strstr(str,”java”);
  printf(“\nSubstring is: %s”,sub);
 return 0;
}
Output:
cloudandclear with c and java

Related Post:

Features of C Programming Language

Variables in C

Data Types in C

Keywords in C

C Operators

Comments in C

Escape Sequence in C

C Functions

Storage Classes in C

Dynamic memory allocation in C

Factorial Program in C

Leap year program in C

Fibonacci Series in C

Prime Number program in C

Palindrome program in C

Sum of digits program in C

Escape Sequence in C

ASCII value in C

Difference Between Type Casting and Type Conversion in C

Difference Between Variables and Constants

Matrix multiplication in C

C Program to generate Fibonacci Triangle

C Program to print “hello” without semicolon

C Program to swap two numbers without third variable

C Program to reverse number

Count the number of digits in C

Tokens in C

C Identifiers

C Strings

Compile time vs Runtime in C

C break statement

C goto statement

Type Casting in C

C Pointers

Dangling Pointers in C

void pointer in C

Pointer to Pointer in C

Recursion in C

Call by value and Call by reference in C

File Handling in C

C fprintf() and fscanf()

C fputc() and fgetc()

C fputs() and fgets()

C fseek() function

Constant Pointers in C

2 thoughts on “C String Functions

  1. Pingback: Data Types in C - CloudNClear

  2. Pingback: Keywords in C - CloudNClear

Comments are closed.