PRACTICAL PROGRAMS:
Program 1: Write a program to replace a character in given string.
#include<stdio.h>
int main( )
{
char s[20], ch, repl_ch;
int i = 0;
printf("Enter a string: ");
gets(s);
printf("Enter character to be replaced: ");
ch = getchar();
printf("Enter replacement character: ");
repl_ch = getchar();
printf(“Modified string after replacement is: “);
while(s[i] != '\0')
{
if(s[i] == ch)
{
s[i] = repl_ch;
}
i++;
}
printf("New string is: ");
puts(s);
return 0;
}
Output:
Enter a string: programming
Enter character to be replaced: m
Enter replacement character: a
Modified string after replacement is:
New string is : prograaaing
Program 2: Write a program to delete a character in given string.
#include<stdio.h>
int main( )
{
char str[50], ch;
int i, j;
printf("Enter String : ");
gets(str);
printf("Enter Character to Delete : ");
scanf("%c", &ch);
for(i = 0 ; str[i] != '\0' ; i++)
{
if (str[i] == ch)
{
for( j = i ; str[j] != '\0' ; j++ )
{
str[j] = str[j+1];
}
i--;
}
}
printf("Final String = %s", str);
return 0;
}
Output:
Enter String : program
Enter Character to Delete : o
Final String = prgram
Program 3: Write a program to reverse string.
#include<stdio.h>
int main( )
{
char s[50], r[50];
int begin, end, i=0, length=0;
printf("Input a string : ");
gets(s);
while(s[i] != '\0')
{
i++;
length++;
}
end=length-1;
for(begin = 0 ; begin < length ; begin++)
{
r[begin] = s[end];
end--;
}
r[begin] ='\0';
printf("Reverse String : %s", r);
return 0;
}
Output:
Input a string : abcde
Reverse String : edcba
Program 4: Write a program to convert string into upper case.
#include<stdio.h>
int main( )
{
char str[50];
int i=0;
printf("Enter String : ");
gets(str);
for(i = 0 ; str[i] != ‘\0’ ; i++)
{
if( str[i]>='a' && str[i]<='z' )
{
str[i] = str[i] - 32;
}
}
printf("Upper Case String = %s", str);
return 0;
}
Output:
Enter String : compuTER
Upper Case String = COMPUTER
PRACTICE PROGRAMS:
Practice Program 1: Write a program to find a character from given string.
#include<stdio.h>
int main( )
{
char str[20], ch;
int i, count=0;
printf("Enter a string : ");
gets(str);
printf("Enter character to search from string : ");
scanf("%c", &ch);
for(i = 0 ; str[i] != ‘\0’ ; i++)
{
if (str[i] == ch)
{
count++;
}
}
if (count == 0)
{
printf("Character %c is not present", ch);
}
else
{
printf("Occurrence of Character %c : %d", ch, count);
}
return 0;
}
Output:
Enter a string : programming
Enter character to search from string : m
Occurrence of character m : 2