Design a site like this with WordPress.com
Get started

Strings in Depth

Strings can be defined as a collection of characters with a null character (‘\0’) at the end.

Strings are written in between double quotes for ex: “Welcome to C Programming”.

Declaration of String

The general form of declaring a string is

char string_name[size];

where string_name is the name of the array & size specifies the no. of characters in a array.

We can initialize a string :-

char str[]="Welcome"

or

char str[]={'W','e','l','c','o','m','e'};

In C , there is no explicit support for string s. Thus, all string functions are implemented in libraries.

Operations on Strings

  • Read/Write Strings.
  • Combine 2 or more Strings
  • Copy one String to other.
  • Compare 2 Strings
  • Extract a sub-string from String.

Reading/Writing Strings

Reading/Writing strings can easily by accomplished by gets() & puts() library functions.

Use of gets() function.

#include<stdio.h>

void main() {
  char string[90];
  printf("Enter the string!");
  gets(string);
  printf("%s", string);
}

Use of puts() function

#include<stdio.h>

void main() {
  char string[90];
  puts("Enter the string!");
  gets(string);
  puts("String is");
  puts(string);
}

Do More with Strings:-

Finding Length of String.

#include<stdio.h>

void main() {
  char string[80];
  int l = 0;
  puts("Enter string!");
  gets(string);
  while (string[l] != '\0')
    l++;
  printf("LENGTH IS : %d", l);
}

String Concatenation

#include<stdio.h>

void main() {
  char string_one[80], string_two[80], string_result[200];
  int i = 0, j = 0;
  puts("Enter string one!");
  gets(string_one);
  puts("Enter string two!");
  gets(string_two);

  while (string_one[i] != '\0')
    string_result[i++] = string_one[i];

  while (string_two[j] != '\0')
    string_result[i++] = string_two[j];

  string_result[i] = '\0';

  printf("Concatenated String is : %s", string_result);
}

Reversing a String

#include<stdio.h>

void main() {
  char string[90], reverse[90];
  printf("Enter string!");
  gets(string);
  int i = 0, j = 0;
  while (string[i] != '\0')
    i++;
  while (--i >= 0)
    reverse[j++] = string[i];
  reverse[j] = '\0';
  printf("Reverse is %s", reverse);
}

Copy String

#include<stdio.h>

void main() {
  char string[90], copied[90];
  printf("Enter string!");
  gets(string);
  int i = 0;
  while (string[i] != '\0')
    copied[i] = string[i++];
  copied[i] = '\0';
  printf("Copied String is %s", copied);
}

Comparing Strings

#include<stdio.h>

void main(){
 char string_one[10],string_two[10];
 int i;
 printf("Enter String One!");
 gets(string_one);
 printf("Enter String Two!");
 gets(string_two);
 i=0;
 while(string_one[i]==string_two[i] && string_one[i]!='\0')
  i++;
 if(string_one[i]==string_two[i])
  printf("Both strings are equal");
 else
  printf("Strings are not equal");
}

If you find the above article helpful, please share this article.

Also any suggestions are welcomed to improve the article.

Thanks for reading !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: