PSUC Labs: Week 7

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

PSUC Labs

Week 7

1. Count the number of words in a sentence.

Code:

#include<stdio.h>
#define MAX_SIZE 100
int main()
{
char str[MAX_SIZE];
int i, words; printf("Raj Shah\n");
printf("Enter any string: "); gets(str);
i = 0;
words = 1;
while(str[i] != '\0')
{
if(str[i]==' ' || str[i]=='\n' || str[i]=='\t')
{
words++;
}
i++;
}
printf("Total number of words = %d\n", words);
return 0;

Output:

2. Input a string and toggle the case of every character in the input string.

Code

#include <stdio.h>
#include <string.h>
int main()
{
char Str[100];
int i;
printf("Raj Shah\n");
printf("Please Enter any String to Toggle : "); gets(Str);
for (i = 0; Str[i]!='\0'; i++)
{
if(Str[i] >= 'a' && Str[i] <= 'z')
{
Str[i] = Str[i] - 32;

}
else if(Str[i] >= 'A' && Str[i] <= 'Z')
{
Str[i] = Str[i] + 32;

}
}
printf("The Given String after Toggling Case of all Characters =
%s\n", Str); return 0;
}

Output:

3. Check whether the given string is a palindrome or not.

Code:

#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int i, len, flag;
flag = 0;
printf("Raj Shah\n”);
printf("Please Enter any String : ");
gets(str);
len = strlen(str);
for(i = 0; i < len; i++)
{
if(str[i] != str[len - i - 1])
{
flag = 1;
break;

}
}
if(flag == 0)
{
printf("'%s' is a Palindrome String\n”, str);

}
else
{
printf("'%s' is Not a Palindrome String\n”, str);

}
return 0;

Output:

4.

Code:

#include <stdio.h>
#include <string.h>
int main()
{
char name[10][8], name2[10][8], temp[8]; int i, j, n;
printf(“Raj Shah\n");
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Enter %d names n: \n", n);
for (i = 0; i < n; i++)
{
scanf("%s", name[i]);
strcpy(name2[i], name[i]);

}
for (i = 0; i < n - 1 ; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(name[i], name[j]) > 0)
{
strcpy(temp, name[i]); strcpy(name[i], name[j]);
strcpy(name[j], temp);
}
}
}
printf("----------------------------------------\n");
printf("Input Names Sorted Names\n");
printf("------------------------------------------\n");
for (i = 0; i < n; i++)
{
printf("%s\t\t%s\n", name2[i], name[i]);
}
printf("------------------------------------------\n");
return 0;

Output :

5. Delete a word from the given sentence

Code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100], word[20];
int i, j, ls, lw, temp, chk=0;
printf("Raj Shah\n");
printf("Enter the String: ");
gets(str);
printf("Enter a Word: ");
gets(word);
ls = strlen(str);
lw = strlen(word);
for(i=0; i<ls; i++)
{
temp = i;
for(j=0; j<lw; j++)
{
if(str[i]==word[j])
i++;
}
chk = i-temp;
if(chk==lw)
{
i = temp;
for(j=i; j<(ls-lw); j++) str[j] = str[j+lw];
ls = ls-lw;
str[j]='\0';

}
}
printf("\nNew String = %s\n", str);
return 0;
}
Output:

5. Delete a word from the sentence

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ()
{
printf("Raj Shah\n");
char str[100], word[100], twoD[10][30];
int i = 0, j = 0, k = 0, len1 = 0, len2 = 0;

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


gets (str);

printf ("Enter the word to be removed:\n");


gets (word);

// let us convert the string into 2D array


for (i = 0; str[i] != '\0'; i++)
{
if (str[i] == ' ')
{
twoD[k][j] = '\0';
k ++;
j = 0;
}
else
{
twoD[k][j] = str[i];
j ++;
}
}

twoD[k][j] = '\0';

j = 0;
for (i = 0; i < k + 1; i++)
{
if (strcmp(twoD[i], word) == 0)
{
twoD[i][j] = '\0';
}
}

j = 0;

for (i = 0; i < k + 1; i++)


{
if (twoD[i][j] == '\0')
continue;

else
printf ("%s ", twoD[i]);
}

printf ("\n");

return 0;
}

Output:

You might also like