What is string in C language?

String in C can also be said as a one-dimensional array of characters which is terminated by a null character (‘\0’). We use string in C to manipulate words or even sentences. We know that each character takes 1 byte in the memory from our data types chapter.

So, a string occupies the number of bytes it contains character as well as one byte for null character. Actually, null character is very important which we have discussed here in this article. The compiler appends a null character at the end of a string when it encounters a sequence of characters.

String declaration and initialization by character array

We can declare and initialize a string by character array. Let’s see the example of string declaration and initialization by character array bellow. Here ‘\0’ is the null character which is the string terminator.

// string declaration and string initialization by character array

char name[]={'C', 'o', 'm', 'p', 'e','t', 'i', 't', 'i', 'v', 'e' '\0'};

char name[20]={'C', 'o', 'm', 'p', 'e','t', 'i', 't', 'i', 'v', 'e' '\0'};

Declaration and initialization by string literal

We can also declare and initialize a string in C by string literal. For this type of string declaration we need not write null character. It will be automatically inserted here. Let’s see the example of string declaration and initialization by string literal.

// string declaration and string initialization by string literal

char name[]="Competitive";    // null character will automatically be inserted

char name[20]="Competitive";

Taking string input and print string output in C

We can take string input from the user and print output using different methods. Here, we have discussed two methods to take string input and print string output. One is using printf() and scanf() and other is using gets() and puts() functions.

Using printf() and scanf()

// Taking string input and print string output using printf() and scanf()
#include <stdio.h>
#include <string.h>
int main(){
  char name[30];
  printf("Enter your name : ");
  scanf("%s", name);

  printf("\nYour name is %s\n",name);
  return 0;
}

Output of this string printing program

c string, string in c programming

Using gets() and puts()

// Taking string input and print string output using gets() and puts()
#include <stdio.h>
#include <string.h>
int main(){
    char yourName[30];
    puts("Enter your Name here : ");

    gets(yourName);    // taking input and storing

    puts(yourName);    // printing output

    return 0;
}

Output of program

Traverse string in C

An important aspect of any programming language is to traverse a string. Traversing a string is not as same as traversing an integer array. We can traverse a string using two way.

  • Traverse using the length of string
  • Traverse using null character

String traversing by length

Let’s see a C program as an example of string traversing by length. It should be mentioned that in this program the null character will be counted as consonant if any.

// string traversing by length
#include<stdio.h>
int main (){
  char s[22] = "competitiveprogrammer";
  int j = 0, firstCounter = 0, secondCounter = 0;

  while(j < 21){
    if(s[j]=='a' || s[j] == 'e' || s[j] == 'i' || s[j] == 'u' || s[j] == 'o'){
      firstCounter ++;
    }else{
      secondCounter++;
    }
  j++;
  }

  printf("The number of vowel is = %d\n",firstCounter);
  printf("The number of consonant is = %d\n",secondCounter);
  return 0;
}

Output of string traversing program

The number of vowel is = 8
The number of consonant is = 13

Process returned 0 (0x0)   execution time : 0.026 s
Press any key to continue.

Traverse string using null character

Here we will traverse a string in C using null character as an identifier of string termination.

// Traverse string using null character
#include<stdio.h>
int main(){
  char s[22] = "competitiveprogrammer";
  int k = 0, vowelCounter = 0, consonantCounter = 0;

  while(s[k] != NULL){
    if(s[k]=='a' || s[k] == 'e' || s[k] == 'i' || s[k] == 'u' || s[k] == 'o'){
      vowelCounter ++;
    }else{
      consonantCounter++;
    }
    k++;
  }

  printf("The number of vowel is = %d\n",vowelCounter);
  printf("The number of consonant is = %d\n",consonantCounter);

return 0;
}

See the output

The number of vowel is = 8
The number of consonant is = 13

Process returned 0 (0x0)   execution time : 0.029 s
Press any key to continue.

Taking long sting as input and print

We can take a long string or a sentence as input and print it using some method. Here we will make some program to serve the purpose. Let’s try to make C program to take long string or a sentence as input and print it.

Read a line of text using fgets() function

We will accept a line of text using fgets() function and use puts() function to print it. Let’s see this program bellow.

// Taking long sting as input and print
#include <stdio.h>
int main(){
  char sentence[100];
  printf("Enter the sentence here : \n");

  fgets(sentence, sizeof(sentence), stdin);

  printf("\nYou have entered the following sentence : \n");
  puts(sentence);

  return 0;
}

Output of this program

Enter the sentence here :
I am learning C language now.

You have entered the following sentence :
I am learning C language now.

Here we can take maximum 100 character as input. You can take more character by increasing the size.

However, we can use gets() function to take long line as input, but we should use fgets(). Because gets() is removed from C standard for its buffer overflow.

Taking long line as input in different way in C

// Taking long line as input in different way in C
#include<stdio.h>
int main(){
  char str[200];
  printf("Enter the the long string : \n");

  scanf("%[^\n]s",str);

  printf("\nYou have entered : \n%s\n",str);

return 0;
}

Output

taking-long-line-as-input-using-c-string-string-in-c-c-string

Passing string to function in C

However, we can pass string to function as like as passing array to function. Now, we will see a program to learn how to pass a string to a function in C.

// Passing string to function in C
#include <stdio.h>

void strDisplay(char str[]){    // function
    printf("\nYou have given the following string : \n");
    puts(str);
}

int main(){
    char myString[50];
    printf("Enter a string : ");
    fgets(myString, sizeof(myString), stdin);
    strDisplay(myString);   //Passing string to the function.
    return 0;
}

Output of string program

Enter a string : I am learning string in C.

You have given the following string :
I am learning string in C.

Use of pointer with string in C

We can use pointers with array, functions or other primitive data types. But here we will learn how to use pointers with string in C. We can use pointers to point a string. Let’s see a C program to understand the use of pointers with a string in C.

// Use of pointer with string in C
#include <stdio.h>
int main(){

  char name[26] = "competititveprogrammer.com";

  char *ptr = name;

  printf("\n%s\n",ptr);

  return 0;
}

See the output bellow

competititveprogrammer.com

Normally we can not copy the content of a string to another directly. Pointer can help us to serve this purpose. Let’s see the program bellow to see the copy of content of string to another.

// copy the content of a string to another in C
#include<stdio.h>
int main(){
  char *firstPtr = "competitive programmer";

  printf("Main string is  : \n%s\n",firstPtr);

  char *secondPtr;
  secondPtr = firstPtr;

  printf("\nCopied string is : \n%s\n",secondPtr);

return 0;
}

Output of this program

Main string is  :
competitive programmer

Copied string is :
competitive programmer

Though we can not reassign a string in C with some another character but we can do this using pointer. Let’s see the program to reassign a string using pointers.

// reassign a string using pointers
#include<stdio.h>
void main (){
  char *myPtr = "competitive";
  printf("Before reassigning the string is : %s\n",myPtr);
  myPtr = "programmer";
  printf("\nAfter reassigning the string is : %s\n",myPtr);
return 0;
}

Output of this string reassign program is as follows.

Before reassigning the string is : competitive

After reassigning the string is : programmer

Previous pageArray to function in C

Next pageString functions in C