Saturday, June 17, 2023

C language - String

 

String

 

Strcpy(S2 ,S1) -  String Copy

s2 copy the s1 string

S2- Destination

S1-   Source

S1-"hai"

Strcpy(S2 ,S1)   

output

hai

Strcat(S1,S2) - String Concatenation

String Concatenation will add two strings

s1 - "hello"

s2 - "world"

output

helloworld

Strcmp(S1,S2) - String Compare

compare two string ,if the two strings are equals the output will be 0.

s1 - "Hello"

s2 - "hello"

output

1

s1 - "hello"

s2 - "hello"

output

0

Strchr(S1,Ch) - String character

s1- "hai"

ch- "a"

output

 found

Strcmpi(S1,S2) - String compare ignore case

string will compare ignores capital and small letter.

only check the words are equal.

s1 - "Hello"

s2 - "hello"

output

0

M=Strlen(S1)  - String length

check how many letters presents in s1

s1-"hello"

output

5

Strlwr(S1) - String lower

s1- "HELlo"

output

hello

Strupr(S1) - String upper

s1- "hello"

output

HELLO

Strstr(S1,S2) - String string

s1- "hello world"

s2 - "hello"

output

found hello

 

strrev (string reverse)

s1="hello"

Output

strrev(s1) = olleh

Example

 

Using Strcpy()Function:

/*To Copy The String From One Variable To Another Variable*/

#include<stdio.h>

#include<conio.h>

#include<string.h>

main()

{

char name [30],str[30];

clrscr();

printf("Enter your name:");

gets(name);  

strcpy(str,name);

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

getch();

}

 

output:

Enter the name: meenakshi

The copied string is:meenakshi

using strlen() & strrev() functions

/* To find the length of the string and reverse the string */

#include<stdio.h>

#include<conio.h>

#include<string.h>

main()

{

char name[30];

int len;

clrscr();

printf("Enter your name:");

gets(name);

len=strlen(name);

strrev(name);

printf("The length of the string is:%d\n",len);

printf("The reversed string is:%s",name);

getch();

}

output:

Enter the name:Bharathi

The length of the string is:8

The reversed string is: ihtarahB

 

 

Using strcpy(),strcmp() & strrev() Functions

/To Find the program whether the given string is Palindrome or not palindrome*/

#include<stdio.h>

#include<conio.h>

#include<string.h>

main()

{

char name[30],string[30],reverse[30];

clrscr();

printf("Enter your name:");

gets(name);

strcpy(string,name);

strcpy(reverse,strrev(name));

if(strcmp(string,reverse)==0)

{

printf("the string %s is Palindrome",string);

}

else

{

printf("the string%s is not Palindrome",string);

}

getch();

}

output1:

Enter your name: sudharsana

the string Sudharsana is not palindrome

output 2:

Enter your name:madam

The string madam is Pallindrome

using strcmpi() Function

/*Compares the two strings*/

#include<string.h>

#include<stdio.h>

#include<conio.h>

main()

{

char value 1[]="INDIA",value2[]="india";

int ptr;

clrscr();

ptr = strcmpi(value1, value2);

if(ptr>0)

{

printf("value 1 is greater than value 2\n");

}

if(ptr<0)

{

printf("value 1 is less than value 2\n");

}

if(ptr==0)

{

printf("value 1 equals value 2\n");

}

getch();

}

output:

value 1 equals value 2.

using strupr() & strlwr() Functions

/*Converts a given string into upper cases as well as lower case*/

 

#include<stdio.h>

#include<conio.h>

#include<string.h>

main()

{

char name[30];

clrscr();

printf("Enter your name:");

gets(name);

printf("The Uppercase string is:%s\n",strupr(name));

printf("the Lowercase string is:%s",strlwr(name));

getch();

}

output:

Enter your name: Ruthra

The Uppercase stromg is:RUTHRA

The Lowercase string is:ruthra

using strcat() & strstr() &strchr()Functions

/* To check acharacter which is persent in a given string and also concatenate the given two strings*/

#include<stdio.h>

#include<conio.h>

#include<string.h>

main()

{

char s1[30],s2[30];

clrscr();

printf("enter the string:");

gets(s1);

printf("Enter the sring:");

gets(s2);

strcat(s1,s2);

if(strchr("Hello",'e'))

{

printf("e is in hello\n");

}

else{

printf("e is not in hello\n");

}

if(strstr(hai everybody","hai"))

{

printf("found hai\n");

}

else

{

printf("Not found hai\n");

}

printf("the concatenated string is:%s\n",s1);

getch();

}

output:

enter the string:program

enter the another string:techie

e is in hello

found hai

the concatenated string is : programtechie

 


0 comments:

Post a Comment