String functions in c with examples

Selva kumar | 220 Views | c programming | 08 Aug 2016


What is a string in c :

  • C Strings are nothing but array of characters ended with null character (‘\0’).
  • This null character indicates the end of the string.
  • Strings are always enclosed by double quotes. 

Declaration of strings :

  • Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char type.
char s[5];
  • Declaration of strings in C language.
  • Strings can also be declared using pointer.

char *p

Example for string in c :

char string[20] = { ‘w’ , ’i’ , ‘k’ , ‘i’ , ‘t’ , ‘e’ , ‘c’ , ‘h’ , ‘y’ , ‘\0’}; (or)

char string[20] = “wikitechy”; (or)

char string []    = “wikitechy”;

  • Difference between above declarations are, when we declare char as “string[20]”, 20 bytes of memory space is allocated for holding the string value.
  • When we declare char as “string[]”, memory space will be allocated as per the requirement during execution of the program.

String using Function in c :

strcat ( ) : Concatenates str2 at the end of str1.

strncat ( ) : Appends a portion of string to another

strcpy ( ) : Copies str2 into str1

strncpy ( ) : Copies given number of characters of one string to another

strlen ( ) : Gives the length of str1.

strcmp ( ) : Returns 0 if str1 is same as str2. Returns <0 if strl < str2. Returns >0 if str1 > str2.

strcmpi_(.) : Same as strcmp() function. But, this function negotiates case.  “A” and “a” are treated as same.

strchr ( ) : Returns pointer to first occurrence of char in str1.

strrchr ( ) : Last occurrence of given character in a string is found

strstr ( ) : Returns pointer to first occurrence of str2 in str1.

strrstr ( ) : Returns pointer to last occurrence of str2 in str1.

strdup ( ) : Duplicates the string

strlwr ( ) : Converts string to lowercase

strupr ( ) : Converts string to uppercase

strrev ( ) : Reverses the given string

Strings as arrays :

  • In C, the abstract idea of a string is implemented with just an array of characters. For example, here is a string:
char label[] = "Single";

  • What this array looks like in memory is the following:
  • Where the beginning of the array is at some location in computer memory, for example, location 1000.

Strings as pointers:

  • Another way of accessing a contiguous chunk of memory, instead of with an array, is with a pointer.
  • Since we are talking about strings, which are made up of characters, we'll be using pointers to characters, or rather, char *'s.
  • However, pointers only hold an address, they cannot hold all the characters in a character array. This means that when we use a char * to keep track of a string, the character array containing the string must already exist (having been either statically- or dynamically-allocated).
  • Below is how you might use a character pointer to keep track of a string.
char label[] = "Single";
char label2[10] = "Married";
char *labelPtr;
labelPtr = label;

String program in c :

int main()
  char s1 [25],s2[25],d[25];
  int l,r;
  printf("\\nEnter First string:");
  printf("\\nEnter Second string:");
  l = strlen(s1);
  printf("\\n1.Length of %s is : %d",s1,l);
  printf("\\n2.String %s is copied in variable d is :%s",s2,d);
  printf("\\n3.After Concatenation : %s",d);
  printf("\\n4.The strings %s,%s are not equal.",s1,s2);
  printf("\\n4.The strings %s,%s are  equal.",s1,s2);
  return 0;

Output :

Enter First string:Wikitechy
Enter Second string: india
1.Length of wikitechy is : 9
2.String india is copied in variable d is :india
3.After Concatenation : Wikitechyindia
4.The strings india,Wikitechy are not equal.