Find the First Capital Letter in a
String without using Recursion
#include
#include
#include
char caps_check(char *);
int main()
{
char string[20], letter;
clrscr();
printf("Enter a string to find its first capital letter: ");
scanf("%s", string);
letter = caps_check(string);
if (letter == 0)
{
printf("No capital letter is present in %s.\n", string);
}
else
{
printf("The first capital letter in %s is %c.\n", string, letter); }
return 0;
}
char caps_check(char *string)
{
int i = 0;
while (string[i] != '\0')
{
if (isupper(string[i]))
{
return string[i];
}
i++;
}
return 0;
getch();
}
Output
Enter a string to find its first capital letter: prOgraMmInG
The first capital letter in prOgraMmInG is O.
Find the Length of the
String using Recursion
#include
#include
#include
struct node
{
int a;
struct node *next;
};
void generate(struct node **);
int length(struct node*);
void delete(struct node **);
int main()
{
struct node *head = NULL;
int count;
generate(&head);
count = length(head);
printf("The number of nodes are: %d\n", count);
delete(&head);
return 0;
}
void generate(struct node **head)
{
int num = 10, i;
struct node *temp;
for (i = 0; i < num; i++)
{
temp = (struct node *)malloc(sizeof(struct node));
temp->a = i;
if (*head == NULL)
{
*head = temp;
(*head)->next = NULL;
}
else
{
temp->next = *head;
*head = temp;
}
}
}
int length(struct node *head)
{
if (head->next == NULL)
{
return 1;
}
return (1 + length(head->next));
}
void delete(struct node **head)
{
struct node *temp;
while (*head != NULL)
{
temp = *head;
*head = (*head)->next;
free(temp);
}
}
Output
The number of nodes are: 10
Check whether a given
String is Palindrome or not using Recursion
#include
#inclue
#include
void check(char [], int);
int main()
{
char word[15];
clrscr();
printf("Enter a word to check if it is a palindrome\n");
scanf("%s", word);
check(word, 0);
return 0;
}
void check(char word[], int index)
{
int len = strlen(word) - (index + 1);
if (word[index] == word[len])
{
if (index + 1 == len || index == len)
{
printf("The entered word is a palindrome\n");
return;
}
check(word, index + 1);
}
else
{
printf("The entered word is not a palindrome\n");
getch();
}
}
}
Output
Enter a word to check if it is a palindrome
malayalam
The entered word is a palindrome
Check whether two Strings
are Anagrams
#include
#include
int find_anagram(char [], char []);
int main()
{
char array1[100], array2[100];
int flag;
clrscr();
printf("Enter the string\n");
gets(array1);
printf("Enter another string\n");
gets(array2);
flag = find_anagram(array1, array2);
if (flag == 1)
printf(""%s" and "%s" are anagrams.\n", array1, array2);
else
printf(""%s" and "%s" are not anagrams.\n", array1, array2);
return 0;
}
int find_anagram(char array1[], char array2[])
{
int num1[26] = {0}, num2[26] = {0}, i = 0;
while (array1[i] != '\0')
{
num1[array1[i] - 'a']++;
i++;
}
i = 0;
while (array2[i] != '\0')
{
num2[array2[i] -'a']++;
i++;
}
for (i = 0; i < 26; i++)
{
if (num1[i] != num2[i])
return 0;
}
return 1;
getch();
}
Output
Enter the string
abll
Enter another string
ball
"abll" and "ball" are anagrams.
Enter the string
tall
Enter another string
all
"tall" and "all" are not anagrams.
No comments:
Post a Comment