0% found this document useful (0 votes)
79 views7 pages

PSUC Labs - Week

This document summarizes code examples from PSUC Labs Week 7. It includes 5 code examples with outputs that: 1. Counts the number of words in a sentence. 2. Toggles the case of every character in an input string. 3. Checks if a given string is a palindrome. 4. Sorts an array of names alphabetically. 5. Deletes a given word from a sentence.

Uploaded by

Raj Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views7 pages

PSUC Labs - Week

This document summarizes code examples from PSUC Labs Week 7. It includes 5 code examples with outputs that: 1. Counts the number of words in a sentence. 2. Toggles the case of every character in an input string. 3. Checks if a given string is a palindrome. 4. Sorts an array of names alphabetically. 5. Deletes a given word from a sentence.

Uploaded by

Raj Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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 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