0% found this document useful (0 votes)
29 views

String Exercise - C Programming

Uploaded by

ovipc2023
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

String Exercise - C Programming

Uploaded by

ovipc2023
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1

CSE114
Programming and Problem Solving Lab
C Programming | String Exercises
1. C program to find the length of a string:
#include <stdio.h>
int main()
{
char str[100];
int i;

printf("Enter a string: ");


gets(str);
int len =0;
for (i = 0; str[i] != '\0'; i++)
{
len++;
}

printf("Length of the string is: %d\n", len);

return 0;
}

2. C program to copy one string to another string:


#include <stdio.h>

int main()
{
char src[100], dest[100];
int i;

printf("Enter a string to copy: ");


gets(src);

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


{
dest[i] = src[i];
}
dest[i] = '\0';

printf("Copied string: %s\n", dest);

return 0;
}
2
3. C program to concatenate two strings:
#include <stdio.h>

int main()
{
char str1[100], str2[100];
int i, j;

printf("Enter first string: ");


gets(str1);

printf("Enter second string: ");


gets(str2);

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

for (j = 0; str2[j] != '\0'; j++, i++)


{
str1[i] = str2[j];
}

str1[i] = '\0';

printf("Concatenated string: %s\n", str1);

return 0;
}

4. C program to compare two strings:


#include <stdio.h>

int main()
{
char str1[100], str2[100];
int i, result = 1;

printf("Enter first string: ");


gets(str1);

printf("Enter second string: ");


gets(str2);
3

for (i = 0; str1[i] != '\0' || str2[i] != '\0'; i++)


{
if (str1[i] != str2[i])
{
result = 0;
break;
}
}

if (result == 1)
{
printf("Strings are equal.\n");
}
else
{
printf("Strings are not equal.\n");
}

return 0;
}

5. Convert a lowercase string to uppercase:


#include <stdio.h>

int main()
{
char str[100];
printf("Enter a string: ");
gets(str);

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


{
if (str[i] >= 'a' && str[i] <= 'z')
{
str[i] = str[i] - 32;
}
}

printf("Uppercase string: %s\n", str);


return 0;
}
4
6. Convert an uppercase string to lowercase:
#include <stdio.h>

int main()
{
char str[100];
printf("Enter a string: ");
gets(str);

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


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

printf("Lowercase string: %s\n", str);


return 0;
}

7. Find the total number of alphabets, digits, or special characters in


a string:

#include <stdio.h>

int main()
{
char str[100];
int alphabets = 0, digits = 0, specialChars = 0;

printf("Enter a string: ");


gets(str);

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


{
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >=
'A' && str[i] <= 'Z'))
{
alphabets++;
}
else if (str[i] >= '0' && str[i] <= '9')
{
5
digits++;
}
else
{
specialChars++;
}
}

printf("Alphabets: %d\n", alphabets);


printf("Digits: %d\n", digits);
printf("Special Characters: %d\n", specialChars);
return 0;
}

8. Count the total number of vowels and consonants in a string:


#include <stdio.h>

int main()
{
char str[100];
int vowels = 0, consonants = 0;

printf("Enter a string: ");


gets(str);

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


{
if (str[i] == 'a' || str[i] == 'e' || str[i] ==
'i' || str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i]
== 'I' || str[i] == 'O' || str[i] == 'U')
{
vowels++;
}
else if ((str[i] >= 'a' && str[i] <= 'z') ||
(str[i] >= 'A' && str[i] <= 'Z'))
{
consonants++;
}
}

printf("Vowels: %d\n", vowels);


printf("Consonants: %d\n", consonants);
return 0;
6
}

9. Count the total number of words in a string:


#include <stdio.h>

int main()
{
char str[100];
int words = 0;

printf("Enter a string: ");


gets(str);

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


{
if ((i == 0 || str[i-1] == ' ') && str[i] != ' ')
{
words++;
}
}

printf("Total number of words: %d\n", words);


return 0;
}

10. Find the reverse of a string:


#include <stdio.h>

int main()
{
char str[100], temp;
int i, j = 0;

printf("Enter a string: ");


gets(str);

while (str[j] != '\0')


{
j++;
}
j--;
7
for (i = 0; i < j; i++, j--)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}

printf("Reversed string: %s\n", str);


return 0;
}

11. Check whether a string is a palindrome:


#include <stdio.h>

int main()
{
char str[100];
int i, j = 0, isPalindrome = 1;

printf("Enter a string: ");


gets(str);

while (str[j] != '\0')


{
j++;
}
j--;

for (i = 0; i < j; i++, j--)


{
if (str[i] != str[j])
{
isPalindrome = 0;
break;
}
}

if (isPalindrome)
{
printf("The string is a palindrome.\n");
}
else
{
printf("The string is not a palindrome.\n");
8
}

return 0;
}
12. Count the frequency of each character in a string:
#include <stdio.h>

int main()
{
char str[100];
int freq[256] = {0};

printf("Enter a string: ");


gets(str);

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


{
freq[(int)str[i]]++;
}

for (int i = 0; i < 256; i++)


{
if (freq[i] > 0)
{
printf("%c: %d\n", i, freq[i]);
}
}

return 0;
}

You might also like