Array
Preprocessor

String

String

string in C is merely an array of characters. The length of a string is determined by a terminating null character: ‘\0’ . So, a string with the contents, say, “abc” has four characters: ‘a’ , ‘b’ , ‘c’ , and the terminating null character. The terminating null character has the value zero.

We can think of strings as an array of characters, like ‘Max‘ is a string.

element‘M’‘a’‘x’‘\0’
index0123

We always put string in ” “.

We can also write string in many different ways.

  1. char string[20] = {‘f’, ’r’, ‘e’, ‘s’, ‘h’, ‘2’, ‘r’, ‘e’, ‘f’, ’r’, ‘e’, ‘s’, ‘h’, ‘\0’};
    (or)
  2. char string[20] = “fresh2refresh”;
    (or)
  3. char string []    = “fresh2refresh”;

Lets take a simple example.

#include
int main()
{
  char str[ ] = "Codingknack"; 
  printf("%s\n", str); 
  return 0;
}

Codingknack

If you want to print all the characters in above string, then we use for loop.

#include
int main()
{
  char str[ ] = "Codingknack"; 
  int i;
  for( i=0; i<12; i++) 
  { 
    printf("%c\n", str[i]); 
  } 
  return 0;
}

C
o
d
i
n
g
k
n
a
c
k

Multi-word String

We can take input and give output of a string that consists of more than one word by using gets and puts, where gets is used to input string from user and puts is used to display the string.

Lets take an example.

#include<stdio.h>
int main()
{
  char name[25]; 
  printf("Enter your name\n");
  gets(name); 
  printf("Your name is "); 
  puts(name);
  return 0;
}

Enter your name
James bread
You name is Sam Bard

Passing Strings to Functions

Lets try an example.

#include<stdio.h>
void display( char ch[])
{
  printf("String :"); 
  puts(ch); /* display string */ 
}
int main() 
{
  char arr[30]; 
  printf("Enter string\n");
  gets(arr); /* input string from user */ 
  display(arr);
  return 0;
}

Enter string 
abcd 
String :abcd

String Functions and their Description.

String functions
Description
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
strset ( )Sets all character in a string to given character
strnset ( )It sets the portion of characters in a string to given character
strtok ( )Tokenizing given string using delimiter

How to initialize 2D Array

There are two ways to initialize a two Dimensional arrays during declaration.

int disp[2][4] = {
    {10, 11, 12, 13},
    {14, 15, 16, 17}
};

Or

int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};

 

Although both the above declarations are valid, I recommend you to use the first method as it is more readable, because you can visualize the rows and columns of 2d array in this method.

Lets try an example.

#include<stdio.h>
int main(){
   /* 2D array declaration*/
   int disp[2][3];
   /*Counter variables for the loop*/
   int i, j;
   for(i=0; i<2; i++) {
      for(j=0;j<3;j++) {
         printf("Enter value for disp[%d][%d]:", i, j);
         scanf("%d", &disp[i][j]);
      }
   }
   //Displaying array elements
   printf("Two Dimensional array elements:\n");
   for(i=0; i<2; i++) {
      for(j=0;j<3;j++) {
         printf("%d ", disp[i][j]);
         if(j==2){
            printf("\n");
         }
      }
   }
   return 0;
}

Enter value for disp[0][0]:1
Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[1][0]:4
Enter value for disp[1][1]:5
Enter value for disp[1][2]:6
Two Dimensional array elements:
1 2 3
4 5 6

Comments are closed.