Anda di halaman 1dari 12

LAB TASKS

1) Write a program that converts all lowercase characters in a


given string to its equivalent uppercase character.
C CODE:
#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
char str[MAX_SIZE];
int i;
/* Input string from user */
printf("Enter your text : ");
gets(str);
for(i=0; str[i]!='\0'; i++)
{
/*
* If current character is lowercase alphabet then
* convert it to uppercase.
*/
if(str[i]>='a' && str[i]<='z')
{
str[i] = str[i] - 32;
}
}
printf("Uppercase string : %s",str);
return 0;
}
OUTPUT

2) Write a program that extracts part of the given string from the
specified position. For example, if the sting is "Working with
strings is fun", then if from position 4, 4 characters are to be
extracted then the program should return string as "king".
Moreover, if the position from where the string is to be
extracted is given and the number of characters to be
extracted is 0 then the program should extract entire string
from the specified position.
C CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
char stri[100];
char stri2[10];
int a,flag=0,i=0,b;

printf("Enter the string: \n");


gets(stri);

printf("Enter a position for sentence: \n");


scanf("%d",&a);
while(flag==0)
{
if(i==(a-1))
{
if(stri[i]==32)
{
i++;
for(b=0;;b++,i++)
{

if(stri[i]==32||stri[i]=='\0')
{
stri2[b]='\0';
break;
}

stri2[b]=stri[i];
}
}

else

for(b=0;;b++,i++)
{

if(stri[i]==32||stri[i]=='\0')
{
stri2[b]='\0';
break;
}

stri2[b]=stri[i];
}
flag=1;
}
i++;
}
printf("\n%s",stri2);
}

OUTPUT

3) Write a program that converts a string like "124" to an


integer124.
C CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main() {
char str[100];
int i;
clrscr();

printf("Enter the string: \n");

gets(str);

i=atoi(str); /* 'atoi' is a function to convert a string into an integer */

printf("The string in integer: \n");

printf("%d",i);

getch();

OUTPUT

4) Write a program that replaces two or more consecutive


blanks in a string by a single blank. For example, if the input
is
Grim return to the planet of apes!!
the output should be
Grim return to the planet of apes!!
C CODE:
#include<iostream>
#include<string.h>

using namespace std;

int main ()
{
char str[80] = "Grim return to the planet of apes!!";

int i=0, len, j;

len = strlen(str);

for( i = 0; i < len; i++)


{
if (str[i] == ' ')
{
for (j = i; j < len; j++)
str[j] = str[j+1];
len--;
}
}

cout << "Resultant string : \n" << str;


return 0;

OUTPUT

5) Write a program to delete all vowels from a sentence. Assume


that the sentence is not more than 80 characters long. Write
a program that will read a line and delete from it all
occurrences of the word ‘the’.
C CODE:
#include <iostream>
#include <cstring>

using namespace std;

int isVowel(char ch);

int main()
{
char input[100], output[100];
int i, j, writeIndex;

cout << "Enter a string: \n";


cin.getline(input, 500);
for(i = 0, j = 0; input[i] != '\0'; i++){
if(!isVowel(input[i])){
output[j++] = input[i];
}
}
output[j] = '\0';

cout << "Input String: " << input << endl;


cout << "String without Vowels: " << output;

return 0;
}

int isVowel(char ch){


switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U': {
return 1;
break;
}
default :
return 0;
}
}

OUTPUT

6) Write a program that takes a set of names of individuals and


abbreviates the first, middle and other names except the last
name by their first letter.
C CODE:
#include <bits/stdc++.h>
using namespace std;

void printInitials(const string& name)


{
if (name.length() == 0)
return;
cout << (char)toupper(name[0]);

for (int i = 1; i < name.length() - 1; i++)


if (name[i] == ' ')
cout << " " << (char)toupper(name[i + 1]);
}

int main()
{
string name = "Nawaf Nadeem";
printInitials(name);
return 0;
}

OUTPUT

7) Write a program to count the number of occurrences of any two


vowels in succession in a line of text. For example, in the sentence
“Pleases read this application and give me gratuity” such
occurrences are ea, ea, ui.
C CODE:
#include <stdio.h>

int isVowel(char);
int main()
{
char str[80];
int i, found = 0, count = 0;

printf("Enter a string: ");


gets(str);

printf("Vowels in successions are :\n");

for (i = 0; str[i] != '\0'; i++)


{
if (isVowel(str[i]))
{
found++;
}
else
{
found = 0;
}

if (found == 2)
{
printf("%c%c\n", str[i - 1], str[i]);
found = 1;
count++;
}
}

printf("\nTotal number of occurences : %d", count++);

return 0;
}
int isVowel(char ch)
{
switch (ch)
{
case 'A':
case 'a':
case 'E':
case 'e':
case 'I':
case 'i':
case 'O':
case 'o':
case 'U':
case 'u':
return 1;
default:
return 0;
}
}

OUTPUT

Anda mungkin juga menyukai