String
String
//write a program to copy a string into another string with using string library function
#include<stdio.h>
#include<string.h>
void main()
{
char source[20],target[20];
printf("enter string\n");
gets(source);
strcpy(target,source);
printf("The source string is \t");
puts(source);
printf("The target string is \t");
puts(target);
}
//write a program to copy a string into another string without using string library
function
#include<stdio.h>
void main()
{
char source[20],target[20];
int i;
printf("enter string\n");
gets(source);
for(i=0;source[i]!='\0';i++)
{
target[i]=source[i];
}
target[i]='\0';
printf("The source string is \t");
puts(source);
printf("The target string is \t");
puts(target);
}
strrev()
Syntax:
strrev(string_name);
//write a program to reverse a string with using string library function
#include<stdio.h>
#include<string.h>
void main()
{
char name[20];
printf("enter string\n");
gets(name);
strrev(name);
printf("after reverse \n");
puts(name);
}
//write a program to reverse without using string library function
#include<stdio.h>
void main()
{
char name[20],temp;
int len=0,i;
printf("enter string\n");
gets(name);
for(i=0;name[i]!='\0';i++)
{
len++;
}
for(i=0;i<len/2;i++)
{
temp=name[i];
name[i]=name[len-1-i];
name[len-1-i]= temp;
}
puts(name);
}
strcat()
strcat(target string, source string);
After concatenation: targetstringsourcestring
//Write a c program to concatenate two string with using library function
#include<stdio.h>
#include<string.h>
void main()
{
char first[20],second[20];
printf("enter ist string\n");
gets(first);
printf("enter 2nd string\n");
gets(second);
strcat(first,second);
printf("after concatenation\n");
printf("ist string\t");
puts(first);
printf("2nd string\t");
puts(second);
}
//Write a c program to check whether given string is palindrome or not without using
library function.
#include<stdio.h>
void main()
{
int p=0,i,len=0;;
char first[20],second[20];
printf("enter string\t");
gets(first);
for(i=0;first[i]!='\0';i++)
len++;
for(i=0;i<len;i++)
{
second[len-1-i] =first[i];
}
second[i]='\0';
for(i=0;i<len;i++)
{
if(first[i]!=second[i])
{
p++;
break;
}
}
if(p==0)
printf("palindrome");
else
printf("not palindrome");
}
//Write a c program to count total no of uppercase letter, lowercase letter , space , digits
and words in a string.
void main()
{
char str[100];
int i,u=0,l=0,s=0,d=0,sp=0;
printf("enter string\t");
gets(str);
for(i=0; str[i] !='\0' ;i++)
{
if(str[i] >=65 && str[i]<90)
u++;
else if(str[i]==32)
s=s+1;
else if(str[i]>=97 && str[i]<=122)
l=l+1;
else if(str[i]>=48 && str[i]<=57)
d=d+1;
else
sp=sp+1;
}
printf("\nnumber of uppercase %d",u);
printf("\nnumber of lowercase %d",l);
printf("\nnumber of digit %d",d);
printf("\nnumber of special character %d",sp);
printf("\nnumber of space %d",s);
}