What is String?
String is a character array ended with a null character.(\0) This is the main difference between a character array and a string. We can perform take input a word or a sentence using this concept and can perform many actions as follows:
The nine most commonly used functions in the string library are:
strcat
– concatenate two stringsstrchr
– string scanning operationstrcmp
– compare two stringsstrcpy
– copy a stringstrlen
– get string lengthstrncat
– concatenate one string with part of anotherstrncmp
– compare parts of two stringsstrncpy
– copy part of a stringstrrchr
– string scanning operation
Here is the manual coding of the inbuilt functions to get the better ideas how string works.
Q1: Write a complete C program to find the length of a String without using function strlen()
#include <stdio.h>
int main()
{
char str[20];
int i,c=0;
/*Input string*/
printf("Enter the string: ");
gets(str);
/*Counting part*/
for(i=0;str[i]!='\0';i++);
printf("\nThe length of the entire string is: %d",i);
return 0;
}
Q2: Write a complete C program to concatenate two strings using third or temp variable but without using strcat().
#include <stdio.h>
int main()
{
char str1[20],str2[20],str3[40];
int i,j;
printf("Enter the string 1: "); //Input 1st string
gets(str1);
printf("Enter the string 2: "); //Input 2nd string
gets(str2);
/*Concatenaing process in a seperate string*/
for(i=0;str1[i]!='\0';i++)
str3[i]=str1[i];
for(j=0;str2[j]!='\0';j++)
str3[i++]=str2[j];
str3[i]='\0';
printf("The new string is: %s",str3);
return 0;
}
Q.3 Write a complete C program to concatenate two strings without using strcat().
#include <stdio.h>
int main()
{
char str1[40],str2[20];
int i,j,l=0;
printf("Enter the string 1: "); //Input 1st string
gets(str1);
printf("Enter the string 2: "); //Input 2nd string
gets(str2);
/*Concatenaing process*/
for(i=0;str1[i]!='\0';i++)
l++;
for(i=l,j=0;str2[j]!='\0';i++,j++)
str1[i]=str2[j];
str1[i]='\0';
printf("The new string is: %s",str1);
return 0;
}
Q.4 Write a complete C program to compare strings using case sensitive and without strcmp() function.
#include <stdio.h>
int main()
{
char str1[20],str2[20];
int i,flag=0;
printf("Enter the 1st string: ");
gets(str1);
printf("Enter the 2st string: ");
gets(str2);
//length of strings
int l1=0,l2=0;
for(i=0;str1[i]!='\0';i++)
l1++;
for(i=0;str2[i]!='\0';i++)
l2++;
//Comparing....case sensative
if(l1==l2)
{
for(i=0;str1[i]!='\0';i++)
{
if(str1[i]!=str2[i])
{
flag=1;
break;
}
}
if(flag==1)
printf("\nThe strings are equal in length but not same.");
else
printf("\nThe strings are completely same.");
}
else
printf("\nThe strings are not equal in length.");
return 0;
}
ALternative Way
#include <stdio.h>
#include<string.h>
int main()
{
char str1[20],str2[20];
int index;
int flag = 1;
int i;
gets(str1);
gets(str2);
puts(str1);
puts(str2);
for(index=0; (str1[index]!='\0' && str2[index]!='\0'); index++)
{
if(str1[index]!=str2[index]){
printf("%d",(str1[index]-str2[index]));
flag=0;
break;
}
}
if(flag==1 && (strlen(str1)==strlen(str2)))
printf("0");
else if (flag==1 && (strlen(str1)>strlen(str2)))
printf("%d",(str1[index]-'\0'));
else if (flag==1 && (strlen(str1)<strlen(str2)))
printf("%d",('\0'-str2[index]));
return 0;
}
Q.5 Write a complete C program to compare strings ignoring case sensitive and strcmp() function.
#include <stdio.h>
int main()
{
char str1[20],str2[20];
int i,flag=0;
printf("Enter the 1st string: ");
gets(str1);
printf("Enter the 2nd string: ");
gets(str2);
//length of strings
int l1=0,l2=0;
for(i=0;str1[i]!='\0';i++)
l1++;
for(i=0;str2[i]!='\0';i++)
l2++;
//Comparing....not case sensative
if(l1 == l2)
{
for(i=0;str1[i]!='\0';i++)
{
if(str1[i]!=str2[i])
{
if (!(str1[i]==str2[i]+32 ||
str1[i]==str2[i]-32))
{
flag=1;
break;
}
}
}
if(flag==1)
printf("\nThe strings are equal in length but not same.");
else
printf("\nThe strings are completely same.");
}
else
printf("\nThe strings are not equal in length.");
return 0;
}
Alternative Way:
#include <stdio.h>
#include<string.h>
int main()
{
char str1[20],str2[20];
int index;
int flag = 1;
int i;
gets(str1);
gets(str2);
for(int index=0; str1[index]!='\0'; index++){
if (str1[index]>=65 && str1[index]<=90){
str1[index]=str1[index]+32;
}
}
for(int index=0; str1[index]!='\0'; index++){
if (str2[index]>=65 && str2[index]<=90){
str2[index]=str2[index]+32;
}
}
puts(str1);
puts(str2);
for(index=0; (str1[index]!='\0' && str2[index]!='\0'); index++)
{
if(str1[index]!=str2[index]){
printf("%d",(str1[index]-str2[index]));
flag=0;
break;
}
}
if(flag==1 && (strlen(str1)==strlen(str2)))
printf("0");
else if (flag==1 && (strlen(str1)>strlen(str2)))
printf("%d",(str1[index]-'\0'));
else if (flag==1 && (strlen(str1)<strlen(str2)))
printf("%d",('\0'-str2[index]));
return 0;
}
Q.6 Write a complete C program to Copy String Without Using strcpy()
#include <stdio.h>
int main()
{
char str1[20],str2[20];
int i;
/* Input the string first*/
printf("Please enter the string 1: ");
gets(str1);
/*Copying part*/
for(i=0;str1[i]!='\0';i++)
str2[i]=str1[i];
str2[i]='\0';
//To make it a string, enter a null character
printf("The copied string is: %s",str2);
return 0;
}
Q.7 Write a complete C program to Convert String to Lowercase without using strlwr()
#include <stdio.h>
int main()
{
char str[20],str1[20];
int i,flag=0;
printf("Enter the string: "); //Input string
gets(str);
//Checking the presence of small letters
for(i=0;str[i]!='\0';i++)
{
if(str[i]>=65 && str[i]<=90)
{
flag=1;
break;
}
}
if(flag==1)
{
for(i=0;str[i]!='\0';i++)
{
if(str[i]>=65 && str[i]<=90)
str1[i]=str[i]+32;
else
str1[i]=str[i];
}
str1[i]='\0';
printf("The revised string is: %s",str1);
}
else
printf("All letters are already in lowercase");
return 0;
}
Q.8 Write a complete C program to Concatenate Two Strings without using strncat()
#include <stdio.h>
void strn(char dest[],char src[],int r)
{
int i,j,l=0;
printf("\nString 1: %s",dest);
printf("\nString 2: %s",src);
for(i=0;dest[i]!='\0';i++)
l++;
for(i=l,j=0;j<r;i++,j++)
dest[i]=src[j];
dest[i]='\0';
printf("\nThe concatenate string is: %s",dest);
}
int main()
{
char str1[20],str2[20];
int i,n,l,r;
printf("Enter string 1: ");
gets(str1);
printf("Enter string 2: ");
gets(str2);
printf("Enter the source string no(1/2): ");
scanf("%d",&n);
printf("Enter range: ");
scanf("%d",&r);
if(n==1)
strn(str2,str1,r);
else if(n==2)
strn(str1,str2,r);
return 0;
}
Q.9 Write a complete C program to find reverse of a String without strrev() function
Using Second String
#include <stdio.h>
int main()
{
char str[20],str1[20];
int i,l=0;
printf("Enter the string: "); //Input string
gets(str);
/*reverse method*/
for(i=0;str[i]!='\0';i++)
l++;
for(i=0;str[i]!='\0';i++)
str1[l-i-1]=str[i];
//Copied from str1 to str
//str is now reversed str
for(i=0;i<l;i++)
str[i]=str1[i];
//Set null manually at the end
str[l]='\0';
str1[l]='\0';
printf("The reversed string is: %s",str);
return 0;
}
Without Using second String
#include <stdio.h>
int main()
{
char str[20];
int len=0,count;
gets(str);
/*reverse method*/
for(len=0;str[len]!='\0';len++);
count = len/2;
for(int i=0;i<count;i++){
char temp = str[i];
str[i] = str[len-i-1];
str[len-i-1]=temp;
}
printf("The reversed string is: %s",str);
return 0;
}
Q.10 Write a complete C program to upper case string without using strupr() function.
#include <stdio.h>
int main()
{
char str[20],str1[20];
int i,flag=0;
printf("Enter the string: "); //Input string
gets(str);
//Checking the presence of small letters
for(i=0;str[i]!='\0';i++)
{
if(str[i]>=97 && str[i]<=122)
{
flag=1;
break;
}
}
if(flag==1)
{
for(i=0;str[i]!='\0';i++)
{
if(str[i]>=97 && str[i]<=122)
str1[i]=str[i]-32;
else
str1[i]=str[i];
}
str1[i]='\0';
printf("The revised string is: %s",str1);
}
else
printf("All letter are already in uppercase");
return 0;
}
Q.11 Write a complete C program to Check A String Is Palindrome Or Not.
#include <stdio.h>
int main()
{
char str1[20];
int flag=0;
int i,l1=0,k=0;
printf("Enter string 1: ");
gets(str1);
for(i=0;str1[i]!='\0';i++)
l1++;
for(i=0;i<l1;i++)
{
if(str1[i]==str1[l1-i-1])
k++;
}
if(k==l1)
printf("\nPalindrome");
else
printf("\nNot palindrome");
for(i=0;i<l1;i++)
{
if(str1[i]!=str1[l1-i-1])
{
flag=1;
break;
}
}
if(flag==0)
printf("\nPalindrome");
else
printf("\nNot palindrome");
return 0;
}
Check Out More
Also Checkout