1.C program to print string one by one characters using loop.
As we know a string can be printed using printf("%s",string_variable)
but in this program we will print string one by one character using
loop.
Here loop is running from 0 (access first character from character array)
to Null (Null is the last character from character array - to terminate the
string.)
/*C program to print string one by one characters using loop.*/
#include<stdio.h>
int main()
{
char text[100];
int i;
printf("Enter any string: ");
gets(text);
printf("Entered string is: ");
for(i=0;text[i]!='\0';i++)
{
printf("%c",text[i]);
}
printf("\n");
return 0;
}
Output:
Enter any string: welcome
Entered string is: welcome
2. Find length of string
The number of characters in the string constitutes the length of string.
For example, length of “computer” is 7.
/* C program to find length of string without using strlen( ) */
#include<stdio.h>
main( )
{
Char str[100];
int i=0,len;
printf(“\n enter the string:”);
gets(str);
while(str[i]!=’\0’)
{
i++;
}
len=i;
printf(“ \n the length of string is: %d”,len);
}
Output:
enter the string: programming
the length of string is: 11
3. Converting characters of string into uppercase
The ASCII code for A-Z varies from 65 – 91 and ASCII code for a – z
ranges from 97 to 123. So, if we have to convert a lower case character
to uppercase, then we need to subtract 32 from the ASCII value of the
character.
/* C program to convert characters of string into uppercase */
#include<stdio.h>
main()
{
char str[100],upper[100];
int i=0,j=0;
printf("Enter any string: ");
gets(str);
printf("Entered string is: %s\n",str);
//convert into upper case
while(str[i]!=”\0”)
{
if(str[i]>='a' && str[i]<='z')
upper[j]=str[i] - 32;
else
upper[i]=str[i];
i++;j++;
}
printf("String in Upper case is: %s\n",upper);
}
Output:
Enter any string: welcome
Entered string is: welcome
String in Upper case is: WELCOME
4. Converting characters of string into lowercase
The ASCII code for A-Z varies from 65 – 91 and ASCII code for a – z
ranges from 97 to 123. So, if we have to convert a lower case character
to uppercase, then we need to add 32 to its ASCII value of the character.
/* C program to convert characters of string into lowercase */
#include<stdio.h>
main()
{
char str[100],lower[100];
int i=0,j=0;
printf("Enter any string: ");
gets(str);
printf("Entered string is: %s\n",str);
//convert into lower case
while(str[i]!=”\0”)
{
if(str[i]>='A' && str[i]<='Z')
upper[j]=str[i] + 32;
else
lower[i]=str[i];
i++;j++;
}
printf("String in lower case is: %s\n",lower);
}
Output:
Enter any string: WELCOME
Entered string is: WELCOME
String in lower case is: welcome
5. Concatenating two strings to form a new string
To concatenate the strings, we have to copy the contents of first string
followed by contents of the second string in a third string. After the
contents have been copied a null character is appended at the end of the
new string.
/* C program to concatenate two strings into new string without using
strcat( ) */
#include <stdio.h>
main()
{
char Str1[100], Str2[100],Str3[100];
int i=0, j=0;
printf("\n Enter the First String : ");
gets(Str1);
printf("\n Enter the Second String : ");
gets(Str2);
//copying contents of string1 to string3
while (Str1[i]!='\0')
{
Str3[j] = Str1[i];
i++;
j++;
}
i=0;
//copying contents of string1 to string3
while (Str2[i]!='\0')
{
Str3[j] = Str2[i];
i++;
j++;
}
Str[j]=’\0’;
printf("\n Concatenated string is = %s", Str3);
}
Output:
Enter the First String: good
Enter the Second String: morning
Concatenated string is = good morning
6. Appending strings
Appending one string to another involves copying the contents of the
source string(S1) at the end destination string(S2). The appending
operation would leave the source string (S1) unchanged and destination
string S2=S2+S1
/* C program to append a string to another string without using strcpy( )
*/
#include <stdio.h>
main()
{
char Str1[100], Str2[100];
int i=0, j=0;
printf("\n Enter the First String : ");
gets(Str1);
printf("\n Enter the Second String : ");
gets(Str2);
while (Str1[i]!='\0')
{
Str3[j] = Str1[i];
i++;
j++;
}
Str2[i]=’\0’;
printf("\n Copied string is = %s", Str2);
}
Output:
Enter the First String : good
Enter the Second String : morning
Copied string is = morninggood
7. Compare two strings
If, S1 and S2 are two strings then comparing two strings will give either
of these results:
a. S1 and S2 are equal
b. S1 > S2, when in dictionary order S1 will come after S2
c. S1 < S2, when in dictionary order S1 precedes S2
To compare the two strings, each and every character is compared from
both the strings. If all characters are same then the two strings are said to
be equal.
/* C program to compare two strings without using strcmp( ) */
#include <stdio.h>
#include<string.h>
void main()
{
char Str1[100], Str2[100];
int i=0,len1=0,len2=0,flag=0;
printf("\n Enter the First String : ");
scanf("%s",Str1);
printf("\n Enter the Second String : ");
scanf("%s",Str2);
len1=strlen(Str1);
len2=strlen(Str2);
if(len1==len2)
{
while (i<len1)
{if(Str1[i] == Str2[i])
i++;
else
break;
}
if(i==len1)
{
flag=1;
printf("two strings are equal");
}
}
else
{
printf("\n two strings are not equal");
}
if(flag==0)
{
if(Str1[i]>Str2[i])
printf(" \n String1 is greater than String2");
else
printf(" \n String2 is greater than String1");
}
}
Output:
Enter the First String : Hello
Enter the Second String : Hello
two strings are equal
Enter the First String : HELLO
Enter the Second String : hello
String2 is greater than String1
Enter the First String : welcome
Enter the Second String : program
String1 is greater than String2
8.Find reverse of string
To reverse a string we just need to swap the first character with the last,
second character with the second last character, and so on.. For example,
S1= “computer” , reverse is “retupmoc”.
/* C program to find reverse of string without using strrev( ) */
#include<stdio.h>
main( )
{
Char str[100],temp;
int i=0,len,j;
printf(“\n enter the string:”);
gets(str);
j=len-1;
while(i<j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
printf(“ \n the reversed string is: %s”,str);
}
Output:
enter the string: computer
n the reversed string is: retupmoc