COMPUTER GAYN
STRING PROGRAMS IN C LANGAUGE
Q1. C program to find the length of
string without using function C?
Ans:-
#include <stdio.h>
int main()
{
char
str[100],i;
printf("Enter
a string: \n");
scanf("%s",str);
// '\0'
represents end of String
for(i=0; str[i]!='\0';
++i);
printf("\nLength
of input string: %d",i);
return 0;
}
Q2. c program to
concate/copy two string without using function in
C?
Ans:-
#include
<stdio.h>
int main()
{
char
str1[50], str2[50], i, j;
printf("\nEnter first string: ");
scanf("%s",str1);
printf("\nEnter second string: ");
scanf("%s",str2);
for(i=0;
str1[i]!='\0'; ++i);
for(j=0;
str2[j]!='\0'; ++j, ++i)
{
str1[i]=str2[j];
}
// \0
represents end of string
str1[i]='\0';
printf("\nOutput: %s",str1);
return 0;
}
Q3. c program to
to count character occurrence in given string?
Ans:-
#include<stdio.h>
#include <string.h>
int main()
{
char
s[1000],c;
int
i,count=0;
printf("Enter the string :
");
gets(s);
printf("Enter character to be searched: ");
c=getchar();
for(i=0;s[i];i++)
{
if(s[i]==c)
{
count++;
}
}
printf("character '%c' occurs %d times \n ",c,count);
return 0 ;
}
Q4. write a c
program to count the number of vowels,consonant,digit
and white
space in a given string?
Ans:-
#include<stdio.h>
void main()
{
char
str[200];
int
i,vowels=0,consonants=0,digits=0,spaces=0,specialCharacters=0;
printf("Enter a string\n");
gets(str);
for(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++;
}
else
if(str[i]>='0' && str[i]<='9')
{
digits++;
}
else if
(str[i]==' ')
{
spaces++;
}
else
{
specialCharacters++;
}
}
printf("\nVowels = %d",vowels);
printf("\nConsonants = %d",consonants);
printf("\nDigits = %d",digits);
printf("\nWhite spaces = %d",spaces);
printf("\nSpecial characters = %d",specialCharacters);
}
Q5. c progrm to
cheak if a given string is palindrome or not?
Ans:-
#include<string.h>
#include<stdio.h>
int main()
{
char
str[10],str1[10];//declaration of the string
int d;
printf("enter
any string \n");
gets(str);
strcpy(str1,str);
strrev(str);
strcmp(str1,str);
if(d ==
0)
{
printf("string
is palindrome");
}
else
{
printf("string
is not palindrome");
}
return
0;
}
0 Comments