« Previous                                                                  Next Chapter »
Strings

String is a collection of characters. When ever user want to work with character type of data, he has to go for character array or string. Both character array and string are same in way of declaration but the difference is the working nature.

To work with character array first we have to specify how many number of char variables we are going to use later we have to enter the data character by character which is burden for user. String eliminates this burden and provides more facility to user when compared to character array.

A character array which terminates with (NULL) character which can be represented with ‘\0’ can be called string. In String user need not to specify how many number of character to be used in prior to the usage. Simply user can enter the data whatever he want to enter. The data will be automatically rearranged in the array. When user press enter key automatically at the end of the data area a specialized character will be stored which can be called as null character will be represented by ‘\0’ .Such a null terminated character array can be called as string. 

To read the data into String ‘C’ provides a separate control string “%s” the problem in “%s” is it treat the space as end of the string means it is unable to identify the data after the space in the input. So we have to use “%[^\n]” to read the data along with spaces which is burden for the user. To eliminate this burden from the user special input and output functions were introduced they are

gets

This function is useful to read the data into the string along with spaces.

Syntax: char* gets(char*); 

puts

This function is useful to print the string  along with spaces.

Syntax: int  puts(char*); 


User may be in need to  perform different operations with character type of data. So we are provided with  a separate header file to perform different types of operations on  strings which can be called as string.h 

Some of the functions in the string.h are


1. strlen

This function is useful to find the length of the string. For that it receives the string variable as its parameter and returns the length of the string.

Syntax: int strlen(char *s);


2. strupr

This function is useful to convert the characters in the string into upper case which are in lower case.

Syntax: char* strlupr(char *s);


3. strlwr

This function is useful to convert the characters in the string into lower case which is in uppercase.

Syntax: char* strlwr(char *s);


4. strrev

This function is useful to reverse the contents in the string.

Syntax: char * strrev(char *s);


5. strdup

This function is useful to produce the duplicate string for the given string.

Syntax: char* strdup(const char*s);


6. strset

This function is useful to set the string with given char.

Syntax: char* strset(char *s, int ch);


7. strnset 

This function is useful to set the string with the given character upto n number positions.

Syntax: char* strnset(char *s, int ch, size_t n); 


8. strcpy

This function is useful to copy one string into another string.

Syntax: char* strcpy( destinationchar *x, sourcechar *y); 


9. strcat

This funtion is useful to concatinate  two strings.

Syntax: char* strcat(char *dest,const char*source);

 

More Examples
  /* program to find the length of the string */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
int l;
char x[40];
clrscr();
printf("enter the name");
gets(x);
printf("the given name is");
puts(x);
l=strlen(x);
printf("the length of the string is %d\n",l);
}

/* program to convert into lowercase */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[40];
clrscr();
printf("enter the name");
gets(x);
printf("the given name is");
puts(x);
strlwr(x);
printf("the final string is %s\n",x);
}

/* program to convert into uppercase */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[40];
clrscr();
printf("enter the name");
gets(x);
printf("the given name is");
puts(x);
strupr(x);
printf("the final string is %s\n",x);
}

/* program to find reverse of the string */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[40];
clrscr();
printf("enter the name");
gets(x);
printf("the given name is");
puts(x);
strrev(x);
printf("the final string is %s\n",x);
}

/* program to duplicate string */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[40];
clrscr();
printf("enter the name");
gets(x);
printf("the given name is");
puts(x);
strdup(x);
printf("the final string is %s\n",x);
}

/* program to set the string with the given character */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[40],ch;
clrscr();
printf("enter the name");
gets(x);
printf("the given name is %s\n",x);
printf("enter the character to be set");
ch=getchar();
strset(x,ch);
printf("the final string is %s\n",x);
}

/* program to set the string with the given character in n characters */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[40],ch;
int n;
clrscr();
printf("enter the string");
gets(x);
printf("the given string is");
puts(x);
printf("enter the character to be set");
ch=getchar();
printf("enter the no of characters to be set");
scanf("%d",&n);
strnset(x,ch,n);
printf("the final string is %s\n",x);
}

/* program to copy the string */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[40];
clrscr();
strcpy(x,"ding");
printf("the final string is %s\n",x);
}

/* program to copy the string from another string */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[40],y[40];
clrscr();
printf("enter the string");
gets(x);
strcpy(y,x);
printf("the final string is %s\n",y);
}

/* program to copy the string from another string */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[40],y[40];
clrscr();
printf("enter the string x");
gets(x);
printf("enter the string y");
gets(y);
strcpy(y,x);
printf("the final string is %s\n",y);
}

/* program to cancatenation of two strings */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[40],y[40];
clrscr();
printf("enter the string x");
gets(x);
printf("enter the string y");
gets(y);
printf("the strings are %s\n %s\n",x,y);
strcat(x,y);
printf("the final string is %s\n",x);
}

/* program to cancatenate the strings based on n */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[40],y[40];
int n;
clrscr();
printf("enter the string x");
gets(x);
printf("enter the string y");
gets(y);
printf("the strings are %s %s\n",x,y);
printf("enter the no of characters to be cantenated");
scanf("%d",&n);
strncat(x,y,n);
printf("the final string is %s\n",x);
}

/* program to compare two strings */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[40],y[40];
int res;
clrscr();
printf("enter the string x");
gets(x);
printf("enter the string y");
gets(y);
printf("the strings are %s %s\n",x,y);
res=strcmp(x,y);
if(res>0)
{
printf("first string is big");
}
else if(res<0)
{
printf("second string is big");
}
else
{
printf("both are equal");
}
}

/* program to find the length of the string without strlen */
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char x[40];
int i=0;
clrscr();
printf("enter the string x");
gets(x);
while(x[i]!='\0')
{
i++;
}
printf("the length is %d\n",i);
}

/* program to convert string into lowercase without strlwr */
#include<stdio.h>
#include<conio.h>
main()
{
char x[40];
int i=0;
clrscr();
printf("enter the string x");
gets(x);
while(x[i]!='\0')
{
if((x[i]>='A')&&(x[i]<='Z'))
{
x[i]=x[i]+32;
}
i++;
}
printf("the final string is %s\n",x);
}

/* program to convert string into uppercase without strupr */
#include<stdio.h>
#include<conio.h>
main()
{
char x[40];
int i=0;
clrscr();
printf("enter the string x");
gets(x);
while(x[i]!='\0')
{
if((x[i]>='a')&&(x[i]<='z'))
{
x[i]=x[i]-32;
}
i++;
}
printf("the final string is %s\n",x);
}

/* program to copy one string into another string */
#include<stdio.h>
#include<conio.h>
main()
{
char x[40],y[40];
int i=0;
clrscr();
printf("enter the string x");
gets(x);
while(x[i]!='\0')
{
y[i]=x[i];
i++;
}
y[i]=x[i];
printf("the final string is %s\n",y);
}

/* program to concatenate the strings without strcat */
#include<stdio.h>
#include<conio.h>
main()
{
char x[40],y[40];
int i=0,j=0;
clrscr();
printf("enter the string x");
gets(x);
printf("enter the string y");
gets(y);
while(x[i]!='\0')
{
i++;
}
while(y[j]!='\0')
{
x[i+j]=y[j];
j++;
}
x[i+j]='\0';
printf("the final string is %s\n",x);
}

/* program to find whether the given string is palindrome or not */
#include<stdio.h>
#include<conio.h>
main()
{
char x[40];
int i=0,j=0;
clrscr();
printf("enter the string x");
gets(x);
while(x[j]!='\0')
{
j++;
}
j--;
while(i<=j)
{
if(x[i]!=x[j])
break;
i++;
j--;
}
if(i>j)
printf("palindrome");
else
printf("not a palindrome");
}



  « Previous                                                                  Next Chapter »