c program print string
This program print a string. String can be printed by using various functions such as printf, puts.
#include<stdio.h>
main()
{
char array[20] = "Hello World";
 printf("%s\n",array);


return 0;
}

To input a string we use scanf function.
#include<stdio.h>
main()
{
  char array[100];
   printf("Enter a string\n");
  scanf("%s",&array);
printf("You entered the string %s\n",array);
return 0;
}


Note that scanf can only input single word strings, to receive strings containing spaces use gets function.




String length
This program prints length of string, for example consider the string "c programming" it's length is 13. Null character is not counted when calculating string length. To find string length we use strlen function of string.h.
C program to find string length
#include<stdio.h>
#include<string.h>
main()
{
 char a[100];
 int length;
printf("Enter a string to calculate it's length ");
gets(a);
length = strlen(a);
printf("Length of entered string is = %d\n",length);
return 0;
}
You can also find string length using pointer or without strlen function. Following program shows how to achieve this.


String length without strlen
C program to find length of a string using pointers.
#include<stdio.h>
main()
{
char array[100], *pointer;
int length = 0;
printf("Enter a string\n");
gets(array);
pointer = array;
while(*(pointer+length))
length++;
printf("Length of entered string = %d\n",length);
return 0;
}


c program to compare two strings
This c program compares two strings using strcmp, without strcmp and using pointers. For comparing strings without using library function see another code below.
C program to compare two strings using strcmp
#include<stdio.h>
#include<string.h>
main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
if( strcmp(a,b) == 0 )
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}


C program to compare two strings without using strcmp
Here we create our own function to compare strings.


int compare(char a[], char b[])
{
int c = 0;
while( a[c] == b[c] )
{
if( a[c] == '\0' || b[c] == '\0' )
break;
c++;
}
if( a[c] == '\0' && b[c] == '\0' )
return 0;
else
return -1;
}


C program to compare two strings using pointers
In this method we will make our own function to perform string comparison, we will use character pointers in our function to manipulate string.


#include<stdio.h>
int compare_string(char*, char*);
main()
{
char first[100], second[100], result;
printf("Enter first string\n");
gets(first);
printf("Enter second string\n");
gets(second);
result = compare_string(first, second);
if ( result == 0 )
printf("Both strings are same.\n");
else
printf("Entered strings are not equal.\n");
return 0;
}
int compare_string(char *first, char *second)
{
while(*first==*second)
{
if ( *first == '\0' || *second == '\0' )
break;
first++;
second++;
}
if( *first == '\0' && *second == '\0' )
return 0;
else
return -1;
}


string copying in c programming
This program copy string using library function strcpy, to copy string without using strcpy see source code below in which we have made our own function to copy string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char source[] = "C program";
char destination[50];
strcpy(destination, source);
printf("Source string: %s\n", source);
printf("Destination string: %s", destination);
getch();
return 0;
}


Copy string without strcpy
: here we copy string by creating our own function which uses pointers.


#include<stdio.h>
void copy_string(char*, char*);
main()
{
char source[100], target[100];
printf("Enter source string\n");
gets(source);
copy_string(target, source);
printf("Target string is \"%s\"\n", target);
return 0;
}
void copy_string(char *target, char *source)
{
while(*source)
{
*target = *source;
source++;
target++;
}
*target = '\0';
}


c program to concatenate strings
This program concatenates strings, for example if the first string is "c " and second string is "program" then on concatenating these two strings we get the string "c program". To concatenate two strings we use strcat function of string.h, to concatenate without using library function see another code below which uses pointers.


#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
  char a[100], b[100];
   printf("Enter the first string\n");
   gets(a);
   printf("Enter the second string\n");
   gets(b);
   strcat(a,b);
   printf("String obtained on concatenation is %s\n",a);
   getch();
   return 0;
}


String concatenation without strcat
#include<stdio.h>
void concatenate_string(char*, char*);
main()
{
    char original[100], add[100];
    printf("Enter source string\n");
    gets(original);
    printf("Enter string to concatenate\n");
    gets(add);
    concatenate_string(original, add);
    Printf("String after concatenation is \"%s\"\n", original);
    return 0;
}
void concatenate_string(char *original, char *add)
{
   while(*original)
      original++;
   while(*add)
   {
      *original = *add;
      add++;
      original++;
   }
   *original = '\0';
}


Reverse string
This program reverses a string entered by the user. For example if a user enters a string "reverse me" then on reversing the string will be "em esrever". We show you three different methods to reverse string the first one uses strrev library function of string.h header file and in second we make our own function to reverse string using pointers and reverse string using recursion. If you are using first method then you must include string.h in your program.


/* String reverse in c*/
#include<stdio.h>
#include<string.h>
main()
{
   char arr[100];
   printf("Enter a string to reverse\n");
   gets(arr);
   strrev(arr);
   printf("Reverse of entered string is \n%s\n",arr);
   return 0;
}

/* Second method */
C program to reverse a string using pointers
: Now we will invert string using pointers or without using library function strrev.
#include<stdio.h>
int string_length(char*);
void reverse(char*);
main()
{
   char string[100];
   printf("Enter a string\n");
   gets(string);
   reverse(string);
   printf("Reverse of entered string is \"%s\".\n", string);
   return 0;
}
void reverse(char *string)
{
   int length, c;
   char *begin, *end, temp;
   length = string_length(string);
   begin = string;
   end = string;
   for ( c = 0 ; c < ( length - 1 ) ; c++ )
      end++;
   for ( c = 0 ; c < length/2 ; c++ )
   {
      temp = *end;
      *end = *begin;
      *begin = temp;
      begin++;
      end--;
   }
}
int string_length(char *pointer)
{
   int c = 0;
   while( *(pointer+c) != '\0' )
      c++;
   return c;
}

C program to reverse a string using recursion

#include<stdio.h>
#include<string.h>
void reverse(char*,int,int);
main()
{
   char a[100];
   gets(a);
   reverse(a, 0, strlen(a)-1);
   printf("%s\n",a);
   return 0;
}
void reverse(char *x, int beg, int end)
{
   char a, b, c;
   if ( beg >= end )
      return;
   c = *(x+beg);
   *(x+beg) = *(x+end);
   *(x+end) = c;
   reverse(x, ++beg, --end);
}