Operations On String


a) Strlen( ) :

  • The strlen() function counts the number of character present in a string.

#include <stdio.h>
	#include <string.h>
	int main(){
		char str1[20]="String";
		char str2[20]={'S','t','r','i','n','g','2','\0'};
		char str3[20];
		printf("Enter a string: ");
		gets(str3);  
		printf("Length of string str1=%d \n",strlen(str1));/*strlen does not count '\0'*/ 
		printf("Length of string str2=%d \n",strlen(str2));   
		printf("Length of string str3=%d",strlen(str3));
		return 0;
	}

output:


Enter a string: NewString
Length of string str1=6
Length of string str2=7
Length of string str3=9


b) Strcpy( ) :

  • This function copies the content of one string into another. The source and targeted string must be supplied to this function.

#include <stdio.h>
#include <string.h>
int main(){
	char input[]="BeHappy";
	char target[20];
	strcpy(target,input);
	printf("input string = %s\n",input);
	printf("target string = %s",target);
	return 0;
}

output:


input string = BeHappy
target string = BeHappy


c)Strcmp( ) :

  • This is a function which compares two strings to find whether they are identical or not
  • The two strings are compared character by character until there is mismatch or end of the any of the strings.If two strings are found to be identical function returns a value zero. If they are not identical it returns the ascii value of mismatched character (i.e. first non-matching pair of character)
									

#include <stdio.h>
#include <string.h>
int main(){
	char str1="jerry";
	char str2="Ferry";
	int i,j,k;
	i=strcmp(str1,jerry);
	j=strcmp(str2,str1);
	k=strcmp(str1,"jerry boy");
	printf("i=%d\nj=%d\nk=%d",i,j,k);
	return 0;
}

output:


i=0
j=4
k=-32 /*ascii value of blank space is 32 */


d) Strcat( ) :

  • This function concatenate one string with other string.
									

#include <stdio.h>
#include <string.h>
int main(){
	char str1="jerry";
	char str2="Ferry";
	int i,j,k;
	i=strcmp(str1,jerry);
	j=strcmp(str2,str1);
	k=strcmp(str1,"jerry boy");
	printf("i=%d\nj=%d\nk=%d",i,j,k);
	return 0; 
}

output:


i=0
j=4
k=-32 /*ascii value of blank space is 32 */

e) Strlwr( ):

  • strlwr function converts all uppercases present in the string to lowercases.

#include <stdio.h>
	#include <string.h>
	int main(){
		 char str1[]="TuToriaLSScHooL";
			  
				printf("%s",strlwr(str1));   //converts to lowercase and displays it.
				return 0;
			
	}

output:


 tutorialsschool

f) Strupr( ) :

  • strupr function converts all lowercase present in the string to uppercases.The resultant from strupr() is stored in the same string.

#include <stdio.h>
	#include <string.h>
	int main(){
		char str1[]="TuToriaLSScHooL";
			  
				printf("%s",strlwr(str1));   //converts to lowercase and displays it.
				return 0;
			
	}

output:


TUTORIALSSCHOOL

g) Strrev( ) :

  • strrev( ) function reverses a given string.

#include <stdio.h>
	#include <string.h>
	int main(){
		char name[30] = "hello";
			 
			   printf("String before strrev() : %s\n",name);
			 
			   printf("String after strrev()  : %s",strrev(name));
			 
			   return 0;
			
	}

output:


String before strrev() : hello
String after strrev()  : olleh

h)Strchr( ):

  • Finds first occurrences of a given character in a string

#include <stdio.h>
	#include <string.h>
	int main(){
		char *p;
			  char str [] = "It is a tutorials website";

			  p = strchr (str, 'w');

			  if (p != NULL)
				printf ("'w' is found at %s\n.", p);

			  return 0; 
			
	}

output:


'w' is found at website.


  • Print Page