Table Of Contents
- 1.write a c code for to find the length of 'str' without using strlen() function and print it.
- 2.write a c code for Count Vowels in a String.
- 3.write a c code for Compare Two Strings
- 4.write a c code for Copy String
- 5.write a c code for Concatenate Two Strings
- 6.write a c code for to convert all uppercase letters to lowercase and all lowercase letters to uppercase
- 7.write a c code for Reverse the String – Method 1
- 8.write a c code for Reverse the String -another Method
- 9.write a c code for Palindrome of the String
1.write a c code for to find the length of ‘str’ without using strlen() function and print it.
#include<stdio.h>
int main()
{
char str[10];
scanf("%s", str);
//Write your code here
int length = 0;
for(int i = 0; str[i] != '\0'; i++)
length++;
printf("%d", length);
return 0;
}
2.write a c code for Count Vowels in a String.
#include<stdio.h>
int main()
{
char str[10];
scanf("%s", str);
//Write your code here
int i, count = 0;
for(i = 0; str[i] != '\0'; i++)
if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u')
count++;
printf("%d", count);
return 0;
}
3.write a c code for Compare Two Strings
#include<stdio.h>
int main()
{
char str1[10], str2[10];
scanf("%s%s", str1, str2);
//Write your code here
int index = 0;
while(str1[index] != '\0' && str2[index] != '\0' && str1[index] == str2[index])
index++;
if(str1[index] == '\0' && str2[index] == '\0')
printf("Yes");
else
printf("No");
return 0;
}
4.write a c code for Copy String
#include<stdio.h>
int main()
{
char str[10], copy_str[10];
scanf("%s", str);
//Write your code here
int i;
for(i = 0; str[i] != '\0'; i++)
{
copy_str[i] = str[i];
}
copy_str[i] = '\0';
printf("Copied string = %s", copy_str);
return 0;
}
5.write a c code for Concatenate Two Strings
#include<stdio.h>
int main()
{
char str1[10], str2[10], str3[20];
scanf("%s%s", str1, str2);
int length = 0, i;
for(i = 0; str1[i] != '\0'; i++)
str3[length++] = str1[i];
for(i = 0; str2[i] != '\0'; i++)
str3[length++] = str2[i];
str3[length] = '\0';
printf("%s", str3);
return 0;
}
6.write a c code for to convert all uppercase letters to lowercase and all lowercase letters to uppercase
#include<stdio.h>
int main()
{
char str[10];
scanf("%s", str);
//Write your code here
for(int i = 0; str[i] != '\0'; i++)
{
if(str[i] >= 'a' && str[i] <= 'z')
str[i] -= 32;
else if(str[i] >= 'A' && str[i] <= 'Z')
str[i] += 32;
}
printf("%s", str);
return 0;
}
7.write a c code for Reverse the String – Method 1
#include<stdio.h>
int length(char str[])
{
int count = 0, i;
for(i = 0; str[i] != '\0'; i++)
count += 1;
return count;
}
int main()
{
char str[10];
scanf("%s", str);
//Write your code here
char reverse_str[10];
int len, i, j = 0;
len = length(str);
for(i = len - 1; i >= 0; i--)
reverse_str[j++] = str[i];
reverse_str[j] = '\0';
printf("%s", reverse_str);
return 0;
}
8.write a c code for Reverse the String -another Method
#include<stdio.h>
int length(char str[])
{
int count = 0, i;
for(i = 0; str[i] != '\0'; i++)
count += 1;
return count;
}
int main()
{
char str[10];
scanf("%s", str);
//Write your code here
int len = length(str);
int lastIndex = len-1;
for(int i = 0; i < len/2; i++)
{
char temp = str[i];
str[i] = str[lastIndex-i];
str[lastIndex-i] = temp;
}
printf("%s", str);
return 0;
}
9.write a c code for Palindrome of the String
#include<stdio.h>
int length(char str[])
{
int count = 0, i;
for(i = 0; str[i] != '\0'; i++)
count += 1;
return count;
}
int main()
{
char str[10];
scanf("%s", str);
//Write your code here
int len = length(str);
int lastIndex = len-1;
int flag = 1;
for(int i = 0; i < len/2; i++)
if(str[i] != str[lastIndex-i])
{
flag = 0;
break;
}
if(flag == 1)
printf("Palindrome");
else
printf("Not Palindrome");
return 0;
}