Strings
Strings
#include<stdio.h>
int main()
{
char str[10];
printf("Enter the String: ");
gets(str);
printf("%s",str);
return 0;
}
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()
int i, j;
gets(Str1);
gets(Str2);
Str3[i] = Str1[i];
Str3[i] = Str2[j];
i++;
}
Str3[i] = '\0';
return 0;
#include <stdio.h>
int main()
int i,j;
gets(str1);
gets(str2);
for(i=0;str1[i]!='\0';i++);
for( j=0;str2[j]!='\0';j++)
str1[i]=str2[j];
i++;
str1[i]='\0';
#include <stdio.h>
#include <string.h>
int main()
int i;
gets(Str1);
gets(Str2);
if(Str1[i] == Str2[i])
else if(Str1[i]>Str2[i])
else
}return 0;
#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--;
return 0;
#include <stdio.h>
#include <string.h>
void main()
char str[100],temp;
int i=0,len;
gets(str);
len=strlen(str)-1;
for(i=len;i>=0;i--)
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()
#include <stdio.h>
#include <string.h>
void main()
char str1[]="science";
int length;
length =strlen(str1);
}
String palindrome
#include <stdio.h>
#include <string.h>
void main()
char str[100];
int i=0,len,r=0;
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
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);
}