String
String
String
Sequence of character is called String.
A string is an array of characters stored in consecutive memory allocation.
In String, the ending character is always null character ('\0').
the null indicate or act as termination of String.
Syntax
char stringname[size];
Initialization
#include<stdio.h>
#include<conio.h>
int main()
{
char s1[50];
printf("\n Enter the String :->");
scanf("%s",&s1);
printf("\n Given String is-->%s",s1);
return 0;
}
----------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
int main()
{
char s1[50];
printf("\n Enter the String :->");
gets(s1);
printf("\n Given String is-->");
puts(s1);
return 0;
}
-------------------------------------------------------------------------
//WAP to read a single character and Display.
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
printf("\n Enter the single character :->");
ch=getchar();
while(s1[i]!='\0')
{
i++;
}
i--;
printf("\n Reverse String-->");
while(i>=0)
{
printf("%c",s1[i]);
i--;
}
return 0;
}
-----------------------------------------------------------------------------------
------------
//WAP to accept String and convert into upper to lower and vice versa...
#include<stdio.h>
int main()
{
char s1[100];
int i=0;
printf("\n Enter the String :-->");
gets(s1);
printf("\n Convert String in upper to lower or lower to upper -->");
while(s1[i]!='\0')
{
if(islower(s1[i]))
printf("%c",toupper(s1[i]));
else if(isupper(s1[i]))
printf("%c",tolower(s1[i]));
else
printf("%c",s1[i]);
i }
return 0;
}
-----------------------------------------------------------------------------------
----
//WAP to accept String and convert into upper to lower and vice versa...
#include<stdio.h>
int main()
{
char s1[100],x;
int i=0;
printf("\n Enter the String :-->");
gets(s1);
printf("\n Convert String in upper to lower or lower to upper -->");
while(s1[i]!='\0')
{
x=s1[i];
if(x>=65 && x<=90)
printf("%c",x+32);
else if(x>=97 && x<=122)
printf("%c",x-32);
else
printf("%c",x);
i++;
}
return 0;
}
--------------------------------------------------------------------------
//WAP to accept two Sring and concat two string.
#include<stdio.h>
int main()
{
char s1[30],s2[30];
int i,j;
printf("\n Enter First String:->");
gets(s1);
return 0;
}
-----------------------------------------------------------------------------------
-
//WAP to check given String is palindrome or not ?
// MADAM , DAD, NITIN, ANNA,
#include<stdio.h>
int main()
{
char s1[50];
int i,j,n,x=1;
printf("\n Enter the String :->");
gets(s1);
n=strlen(s1);
for(i=0,j=n-1; i<=j; i++,j--)
{
if(s1[i]!=s1[j])
{
x=0;
break;
}
}
if(x==1)
printf("\n Given String is palindrome");
else
printf("\n Given String is NOT palindrome");
return 0;
}
----------------------------------------------------------------------------------
//WAP to accept two Sring and copy from one string to another string.
#include<stdio.h>
int main()
{
char s1[30],s2[30];
int i=0;
printf("\n Enter First String:->");
gets(s1);
while(s1[i]!='\0')
{
s2[i]=s1[i];
i++;
}
s2[i]='\0';
-----------------------------------------------------------------------------------
----
//WAP to accept String and encrypt its and decrypt its.
#include<stdio.h>
int main()
{
char s1[30];
int x=10,i;
printf("\n Enter String:->");
gets(s1);
printf("\n Enter the code to be encrypt-->");
scanf("%d",&x);
for(i=0; s1[i]!='\0'; i++)
{
s1[i] =s1[i]+x;
}
printf("\n Encrypted message-->"); puts(s1);
return 0;
}
----------------------------------------------------------------------------------
Operation Possible on String
i) To find length of String.
ii) To Copy content of one String to another String.
iii) To change the case of letters in the String.
iv) Reverse of String.
v) To encryption and decryption operation with String.
vi) To find out Substring from string.
vii) Rearrangeing and manipulation of String.
viii) To Searching of string.
ix) Compare Two String.
x) Concatenation(join) of two String
xi) Replace a single character in String.
-----------------------------------------------------------------------------------
--
String Handling Function.
C lang provide lots of predefine String handling function declare in "string.h"
header file.
-----------------------------------------------------------------------------------
-------------------------------------
Function | Meaning
--
-----------------------------------------------------------------------------------
-----------------------------------
1. strlen(s1) | Return length of String s1.
-----------------------------------------------------------------------------------
-------------------------------------
2. strlwr(s1) | Convert String s1 into the lowercase
-----------------------------------------------------------------------------------
-------------------------------------
3. strupr(s1) | Convert String s1 into the Uppercase
-----------------------------------------------------------------------------------
-------------------------------------
4. strrev(s1) | Convert String s1 in reverse order
-----------------------------------------------------------------------------------
-------------------------------------
5. strcat(s1,s2) | Appends a copy of string s2 to the end of string
s1.
-----------------------------------------------------------------------------------
-------------------------------------
6. strcmp(s1,s2) | Compare String s1 and s2. return '0' if s1=s2,
return -ve if s1<s2
return +ve if s1>s2
-----------------------------------------------------------------------------------
-------------------------------------
7. strcmpi(s1,s2) | Compare String s1 and s2. ignoreing the case and
return same result as
strcmp.
-----------------------------------------------------------------------------------
-------------------------------------
8. strncmp(s1,s2,n) | Compare First 'n' chars in String s1 and s2. return
similar result as
strcmp().
-----------------------------------------------------------------------------------
-------------------------------------
9. strcpy(s1,s2) | Copies the String s2 into String s1, and modify
String s1.
-----------------------------------------------------------------------------------
-------------------------------------
10. strchr(s1,ch) | Return a pointer to a the first occurence of char
ch in String s1.
-----------------------------------------------------------------------------------
-------------------------------------
11. strrchr(s1,ch) | Return a pointer to a the LAST occurence of char
ch in String s1.
-----------------------------------------------------------------------------------
-------------------------------------
12. strstr(s1,s2) | Return a pointer to a the first occurence of
String s2 in String s1.
-----------------------------------------------------------------------------------
-------------------------------------
13. strtok(s1,s2) | Search s1 for token that are separately by
delimiters specified in S2.
| Return the pointer to first character of
first token in s1.
-----------------------------------------------------------------------------------
-------------------------------------
14. strset(s1,ch) | Sets all characters in String s1 to characters ch
and quits when null char is detected.
-----------------------------------------------------------------------------------
-------------------------------------
15. strnset(s1,ch,n) | Sets the first 'n' characters in String s1 to chars
ch and quits when null
| chars is detected. if n> length of s1,
then length of s1 replaces by 'n'.
-----------------------------------------------------------------------------------
-------------------------------------
//WAP to demonstrate of String function.
#include<stdio.h>
#include<string.h>
int main()
{
char s1[100];
printf("\n Enter the String -->");
gets(s1);
printf("\n Given String is -->");
puts(s1);
return 0;
}
================================================================
//WAP to check Given String is palindrome or not ?
#include<stdio>
#include<conio.h>
int main()
{
char s[30];
int i,j,n,x=1;
printf("\n Enter the String:->");
gets(s);
n=strlen(s);
for(i=0, j=n-1; i<=j; i++,j--)
{
if(s[i]!=s[j])
{
x=0;
break;
}
}
if(x==1)
printf("\n Given String is palindrome");
else
printf("\n Given String is NOT palindrome");
return 0;
}
=============================================================================
//WAP to demonstrate of String strcmp(s1,s2) function
#include<stdio.h>
#include<string.h>
int main()
{
int x;
char s1[50],s2[50];
printf("\n Enter the First String-->"); gets(s1);
printf("\n Enter the Second String-->"); gets(s2);
x= strcmp(s1,s2);
if(x==0)
printf("\n Both String are equals");
else if(x>0)
printf("\n First String > Second String");
else if(x<0)
printf("\n First String < Second String");
else
printf("\n Kalatey ka ? ");
strcat(s1,s2);
printf("\n Concatenation of String -->"); puts(s1);
}
-----------------------------------------------------------------------------------
------------------------------------
//WAP to demonstrate of String function
#include<stdio.h>
#include<string.h>
int main()
{
int x;
char s1[50],s2[50];
printf("\n Enter the First String-->"); gets(s1);
printf("\n Enter the Second String-->"); gets(s2);
x= strncmp(s1,s2,4);
if(x==0)
printf("\n Both String are equals");
else if(x>0)
printf("\n First String > Second String");
else if(x<0)
printf("\n First String < Second String");
else
printf("\n Kalatey ka ? ");
strcat(s1,s2);
printf("\n Concatenation of String -->"); puts(s1);
}
------------------------------------------------------------------------------
//WAP to demonstrate of strcat() String function
#include<stdio.h>
#include<string.h>
int main()
{
int x;
char s1[50],s2[50];
printf("\n Enter the First String-->"); gets(s1);
printf("\n Enter the Second String-->"); gets(s2);
strcpy(s2,s1);
return 0;
}
-----------------------------------------------------------------------------------
--
//WAP to count each vowel in given String.
#include<stdio.h>
#include<string.h>
void countvowel(char p[]) //defination
{
int j=0,a=0,e=0,i=0,o=0,u=0;
char x;
while(p[j]!='\0')
{
x=p[j];
switch(x)
{
case 'a': case 'A': a++; break;
case 'i': case 'I': i++; break;
case 'e': case 'E': e++; break;
case 'o': case 'O': o++; break;
case 'u': case 'U': u++; break;
}
j++;
}
printf("\n Num of A=%d \n Num of E=%d \n Num of I=%d",a,e,i);
printf("\n Num of O=%d\n Num of U=%d",o,u);
}
int main()
{
char s1[100];
printf("\n Enter the String :-->");
gets(s1);
printf("\n Count vowels in String:-->");
countvowel(s1); //calling
return 0;
}
-----------------------------------------------------------------------------------
--------------------------
//WAP to accept String and encrypt and decrypt of given String...
#include<stdio.h>
void encrypt(char p[],int y)
{
int i=0;
while(p[i]!='\0')
{
p[i]=p[i]+y;
i++;
}
}
void decrypt(char p[],int y)
{
int i=0;
while(p[i]!='\0')
{
p[i]=p[i]-y;
i++;
}
}
int main()
{
char s1[100];
int x;
printf("\n Enter the String :-->");
gets(s1);
printf("\n Enter the private key:-->");
scanf("%d",&x);
printf("\n Given String:-->"); puts(s1); //calling
encrypt(s1,x);
printf("\n Encrypted String:-->"); puts(s1); //calling
decrypt(s1,x);
printf("\n Decrypted String:-->"); puts(s1); //calling
return 0;
}
-----------------------------------------------------------------------------------
------------------------
#include<stdio.h>
int main()
{
int i;
char s1[]="Programming with C";
for(i=0; s1[i]!='\0'; i++)
{
if(i%2==0)
printf("%c%c",s1[i],s1[i]);
}
return 0;
}
-------------------------------------------------------------------
#include<stdio.h>
int main()
{
int i;
char s1[]="Programming with C";
for(i=0; s1[i]!='\0'; i++)
{
if(i%2==0)
printf("%c%c",s1[i],s1[i]);
}
return 0;
}
//Output PPoorrmmiiggwwwtt
----------------------------------------------------------------------
#include<stdio.h>
int main()
{
int i;
char s[]="Rahul's Academy";
for(i=0; s[i]; i++)
{
printf("\n %c",i[s]);
}
return 0;
}
-------------------------------------------------------------------
#include<stdio.h>
int main()
{
int i;
for(;;)
{
printf(" Happy Birthday Dear");
}
return 0;
}
----------------------------------------------------------------------------
#include<stdio.h>
int main()
{
char ch='A';
int i=2;
float f = ++ch + i;
printf(" %f %d %c ",f,ch,ch);
return 0;
}
--------------------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[50],ch,*ptr;
clrscr();
printf("\n Enter the string:->");
gets(s1);
ptr = strchr(s1,ch);
if(ptr)
printf("The character %c is at position: %d\n", ch, ptr-str);
else
printf("The character was not found\n");
getch();
}
-----------------------------------------------------------------------------
//WAP to demonstrate of strtok (String tokonization...)
#include<stdio.h>
#include<string.h>
int main()
{
char s1[50],*p;
p=strtok(s1," ");
while(p!=NULL)
{
printf("\n%s",p);
p=strtok(NULL," ");
}
return 0;
}
----------------------------------------------------------------------------
//WAP to demonstrate of strstr (substring...)
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char s1[50],s2[50],*p;
p=strrchr(A,ch); //p=strrchr(A,ch);
n=p-A;