Strings in C

Hope you are doing great! In this post, we are going to discuss strings in C. We will try to give you, an overview of what strings are and where do we actually use strings.

Introduction

The string in C programming language is actually a one-dimensional array of characters that is
terminated by a null character ‘\0’. Thus, a null-terminated string includes the characters that
comprise the string followed by a null.

Declaration of strings: Declaring a string is the same as declaring a one-dimensional array. Syntax:

char str_name[size];

Reading and Displaying Strings


In the following example, we will see the declaration and initialization of a string consisting of the word “Welcome”. To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word “Welcome”.

char greet[8] = {'W', 'e', 'l', 'c', 'o', 'm', 'e' ,'\0'};

If you follow the rule of array initialization then you can write the above statement as follows:

char greeting[] = "Welcome";

Actually, you do not place the null character at the end of a string constant. The C compiler
automatically places the ‘\0’ at the end of the string when it initializes the array.

Operations performed on character strings include-

  1. Reading and Writing strings
  2. Copying one string to another
  3. Combining strings together
  4. Comparing strings for equality
  5. Extracting a portion of a string

C offers four main operations on strings-

  1. strcpy – copy one string into another
  2. strcat – append one string onto the right side of the other
  3. strcmp — compare alphabetic order of two strings
  4. strlen — return the length of a string

We will understand each function/operation in a detailed manner.

String Handling functions

C library supports a large number of string handling functions. Such functions are stored under
the header file string.h in the program.
Let us see about some of the string handling functions.

strlen

We use strlen() to return the length of the string, which means counting the number
of characters present in a string.
Syntax:
integer variable = strlen (string variable);
Example:

#include<stdio.h>
#include<conio.h>
void main()
{
char str[20];
int strlength;
clrscr();
printf(‚Enter String:‛);
gets(str);
strlength=strlen(str);
printf(‚Given String Length Is: %d‛, strlength);
getch();
}

Output:
Enter String
Welcome
Given String Length Is:7

gets

The gets() function enables the user to enter some characters followed by the enter key. The characters entered by the user get stored in a character array. The null character is added to the array to make it a string. The gets() allows us to enter the space-separated strings. 

Example:

#include<stdio.h>  
void main ()  
{  
    char s[30];  
    printf("Enter the string? ");  
    gets(s);  
    printf("You entered %s",s);  
}  

Output

Enter the string? 
javatpoint is the best
You entered javatpoint is the best

The gets() function is risky to use since it doesn’t perform any array bound checking and keep reading the characters until the new line (enter) is encountered. 

fgets

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored in the fgets (“file get string”) function is similar to the gets function.

Rather than reading a string from standard input, fgets reads it from a specified stream, usually file stream up to and including a newline character. It stores the string in the string variable passed to it, adding a null character to terminate the string.

Syntax:

char *fgets(char *str, int n, FILE *stream)

getchar

getchar() as the name states reads only one character at a time. In order to read a string, we have to use this function repeatedly until a terminating character is encountered. The characters scanned one after the other have to be stored simultaneously into the character array.

Using getchar(), string can be read as follows:

#include <stdio.h>

int main()

{

char str[50], ch;

int i;

printf("Enter a string: ");

i = 0;

ch = getchar ();

while(ch!='\n')

{

str[i] = ch;

i++;

ch = getchar();

}

str[i] ='\0';

printf("Entered string is: %s", str);

return 0;

}

Output:

Enter a string: Where have you e# #been?

Entered string is: Where have you been?

Here we have used the new line ‘\n’ character to be the terminating character. Note that, you have to manually insert the null ‘\0’ character at the end of the string using this method.

puts

The puts() function prints the string on the console which is previously read by using gets() or scanf() function. Since, it prints an additional newline character with the string, which moves the cursor to the new line on the console, the integer value returned by puts() will always be equal to the number of characters present in the string plus 1.

Example-

#include<stdio.h>  
#include <string.h>    
int main(){    
char name[50];    
printf("Enter your name: ");    
gets(name); //reads string from user    
printf("Your name is: ");    
puts(name);  //displays string    
return 0;    
}    

Output-

Enter your name: Naval Ravikant
Your name is: Naval Ravikant

putchar

putchar() as the name states prints only one character at a time. In order to print a string, we have to use this function repeatedly until a terminating character is encountered.

Using putchar(), string can be read as follows:

i = 0;

while(str[i]!='\0')

{

putchar( str[i] );

i++;

}

Conclusion

In this post we have studied strings in C and where do we use them.

  1. 7 ways to convert int to string in java
  2. Concatenate two string in C
  3. Codemate String editorial
  4. A Good string editorial
  5. String anagram in C

Leave a Comment

Your email address will not be published. Required fields are marked *