0% found this document useful (0 votes)
15 views

Strings

Uploaded by

Kavya Shree ds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Strings

Uploaded by

Kavya Shree ds
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Read and display of string

#include<stdio.h>
int main()
{
char str[10];
printf("Enter the String: ");
gets(str);
printf("%s",str);
return 0;
}

Length of the string


#include<stdio.h>
int main()
{
char str[10];
int i,

printf("Enter the String: ");


gets(str);

for (i = 0; str[i] != '\0'; ++i);

printf("Length of Str is %d",i);

return 0;
}
Find the ascii value of a character
#include<stdio.h>
int main()
{
char ch='A';
int i;
i=ch;
printf("%d",i);
return 0;
}

String Operations
1. UPPER to lower
int main()
{
char str[30];
printf("Enter your String(Upper case):");
gets(str);
int i = 0;
//convert capital letter string to small letter string
for(i=0;str[i]!='\0';i++)
{
if(str[i]>='A' && str[i]<='Z')
str[i] += 32;
}
printf("Lower case String is:%s", str);
}

2. Lower to UPPER
int main()
{
char str[30];
printf("Enter your String:");
gets(str);
int i = 0;
for(i=0;str[i]!='\0';i++)
{
if(str[i]>='a' && str[i]<='z')
str[i] -= 32;
}
printf("Upper case String is:%s", str);
}

3. String Concatenate

#include <stdio.h>

#include <string.h>

int main()

char Str1[100], Str2[100],Str3[100];

int i, j;

printf("\n Please Enter the First String : ");

gets(Str1);

printf("\n Please Enter the Second : ");

gets(Str2);

for (i = 0; Str1[i]!='\0'; i++)

Str3[i] = Str1[i];

for (j = 0; Str2[j]!='\0'; j++)

Str3[i] = Str2[j];

i++;
}

Str3[i] = '\0';

printf("\n After the Concatenate = %s", Str3);

return 0;

Append two strings

#include <stdio.h>

int main()

char str1[20]; // declaration of char array variable

char str2[20]; // declaration of char array variable

int i,j;

printf("Enter the first string");

gets(str1);

printf("\nEnter the second string");

gets(str2);

for(i=0;str1[i]!='\0';i++);

for( j=0;str2[j]!='\0';j++)

str1[i]=str2[j];

i++;

str1[i]='\0';

printf("After append, the string would look like: %s", str1);

Compare two Strings

#include <stdio.h>

#include <string.h>
int main()

char Str1[100], Str2[100];

int i;

printf("\n Please Enter the First String : ");

gets(Str1);

printf("\n Please Enter the Second String : ");

gets(Str2);

for(i = 0; Str1[i] == Str2[i] && Str1[i]!='\0'; i++);

if(Str1[i] == Str2[i])

printf("\n Both are Equal");

else if(Str1[i]>Str2[i])

printf("\n str1 is greater than str2");

else

printf("\n str2 is greater than str1");

}return 0;

Reversing the string

#include <string.h>

int main()

char str[100],temp;

int i=0,j=0;
printf("\n Enter the String : ");

gets(str);

j=strlen(str)-1;

for(i=0;i<j;i++)

temp=str[j];

str[j]=str[i];

str[i]=temp;

j--;

printf("The reverse string %s", str);

return 0;

Reverse – different way

#include <stdio.h>

#include <string.h>

void main()

char str[100],temp;

int i=0,len;

printf("\n Enter the String : ");

gets(str);

len=strlen(str)-1;

printf("The reverse of string is");

for(i=len;i>=0;i--)

printf(" %c", str[i]);

With string functions

a. Strcat()
#include <stdio.h>

#include <string.h>

void main()

char str1[10]="computer";

char str2[20]="science";

strcat(str1,str2);

printf("%s",str1);

b. Strcpy()

#include <stdio.h>

#include <string.h>

void main()

char str1[10];

char str2[20]="science";

strcpy(str1,str2);

printf("%s",str1);

c. Strlen()

strlen() computes string's length,

#include <stdio.h>

#include <string.h>

void main()

char str1[]="science";

int length;

length =strlen(str1);

printf("length of string is %d",length);

}
String palindrome

#include <stdio.h>

#include <string.h>

void main()

char str[100];

int i=0,len,r=0;

printf("\n Enter the String : ");

gets(str);

len=strlen(str)-1;

for(i=0;i<len;i++)

if(str[i]!=str[len-i])

r=1;

if(r==0)

printf("%s is a palindrome",str);

else

printf("%s is not palindrome",str);

d. Strcmp
The strcmp() compares two strings character by character. If the strings are equal,
the function returns 0.
Syntax:
int strcmp (const char* str1, const char* str2);
#include <stdio.h>
#include<string.h>
int main()
{
char str1[]="CSE";
char str2[]="CSE";
int result;
result=strcmp(str1,str2);
printf("%d", result);
}

You might also like