Wa0001 PDF
Wa0001 PDF
#include <stdio.h>
int main()
{
char s[1000];
int i=0;
printf("Enter String: ");
scanf("%s", s);
/* After the for loop semicolon is there to calculate total length of string
and after this for no statement to execute*/
for(i = 0; s[i] != '\0'; i++);
printf("Length of String: %d", i);
return 0;
}
C program to read string and count total number of "I" or "i" in that string.
#include <stdio.h>
int main()
{
char s[1000];
int c=0,j;
printf("Enter String: ");
gets(s);
for(j = 0; s[j] != '\0'; j++)
{
if(s[j]=='i' || s[j]=='I')
c++;
}
printf("\nNumber of I/i in String = %d", c);
return 0;
}
C program to read string and count total number of digits in that string.
#include <stdio.h>
int main()
{
char s[1000];
int c=0,j;
printf("Enter string: ");
gets(s);
for(j = 0; s[j] != '\0'; j++)
{
if(s[j]=='0' || s[j]=='1'|| s[j]=='2'||
s[j]=='3'|| s[j]=='4'|| s[j]=='5'||
s[j]=='6'|| s[j]=='7'|| s[j]=='8'|| s[j]=='9')
c++;
}
printf("\nNumber of digits in string = %d", c);
return 0;
}
C program to read a string and count total number of digits, alphabets and special
characters.
#include <stdio.h>
int main()
{
char string[100];
int a, d, o, i;
a = d = o = i = 0;
/* Reads a string from user. Max size of string is 100*/
printf("Enter any string : ");
gets(string);
/* Checks each character of string */
while(string[i]!='\0')
{
if((string[i]>='a' && string[i]<='z') || (string[i]>='A' &&
string[i]<='Z'))
{
a++;
}
else if(string[i]>='0' && string[i]<='9')
{
d++;
}
else
{
o++;
}
i++;
}
printf("Alphabets = %d\n", a);
printf("Digits = %d\n", d);
printf("Special characters = %d\n", o);
return 0;
}
Write a C program to accept a string from user, delete all vowels and display the
result.
#include<string.h>
int main()
{
char s[20];
int len, i, j;
printf("Enter String : ");
gets(s);
len=strlen(s);
for(i=0; i<len; i++)
{
if(s[i]=='a' || s[i]=='e' || s[i]=='i' ||
s[i]=='o' || s[i]=='u' || s[i]=='A' ||
s[i]=='E' || s[i]=='I' || s[i]=='O' ||s[i]=='U')
{
for(j=i; j<len; j++)
{
s[j]=s[j+1];
}
len--;
}
}
printf("\nString After Deleting Vowels : %s",s);
return 0;
}
Assignment program:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int a;
char str1[30],str2[30],str3[30],z;
int i=0,c=0;
//while(1)
do
{
printf("\t\tMENU\n");
printf("----------------------------------------------");
printf("\n1. Check String is Substring of Another String\n");
printf("\n2. Count Occurrences of Character \n");
printf("\n3. Exit\n");
printf("\nEnter Your choice: ");
scanf("%d",&a);
switch(a)
{
case 1:
printf("\nEnter First String:");
scanf("%s",str1);
printf("\nEnter Second String:");
scanf("%s",str2);
if(strstr(str1,str2)==NULL)
printf("\nSecond String is Not Substring of First String\n\n");
else
printf("\nSecond String is Substring of First String\n\n");
break;
case 2:
printf("\nEnter String: ");
scanf("%s",str3);
printf("\nEnter Character : ");
scanf("%s",&z);
for(i=0;str3[i]!='\0';i++)
{
if(z==str3[i])
c++;
}
printf("\nNumber of Occurrences of '%c' : %d\n\n",z,c);
break;
case 3:
exit(0);
default:
printf("\nNot Valid Choice\n\n");
}
}while(a!=3);
return 0;
}
Strupr,Strlwr operations:
In this program, we will convert string in upper case and lower case. First we will
read a string and then convert string in
upper case and after printing the string that is converted into upper case, we will
convert it in lower case and print the lower case.
Logic behind to implement this program - Just check the alphabets if they are in
upper case add the value 32 to convert the character
into lower case otherwise subtract 32 [0x20 - hex value] to convert the character
into upper case.
Because difference between in the ascii codes of upper case and lower case
characters are 32
#include<stdio.h>
int main()
{
char text[100];
int i;
return 0;
}
Strlen operations:
#include <stdio.h>
void main()
{
char str1[50];
int i, l = 0;
strrev operations:
#include<stdio.h>
#include<string.h>
void main()
{
int i,n;
char str[20];
printf("Enter the String to get reversed\n");
gets(str);
n=strlen(str);
printf("\nReversed string is \n");
for(i=n-1;i>=0;i--)
{
printf("%c",str[i]);
}
strcpy operation:
#include <stdio.h>
int main() {
char s1[100], s2[100], i;
printf("Enter string s1: ");
fgets(s1, sizeof(s1), stdin);
s2[i] = '\0';
printf("String s2: %s", s2);
return 0;
}
Strcmp operation:
#include<stdio.h>
void main()
{
char s1[20]="Hello world";
char s2[20]="Hello world";
int flag=0,i=0;
while (s1[i] != '\0' || s2[i] != '\0')
{
if (s1[i] == s2[i])
{i++;}
else if ((s1[i] == '\0' && s2[i] != '\0')|| (s1[i] != '\0' && s2[i] == '\
0')|| s1[i] != s2[i])
{
flag = 1;
break;
}}
if(flag==0)
printf("Equal");
else
printf("Not Equal");
}
Strcat operation:
#include <stdio.h>
int main()
{
char str1[100] = "Hello", str2[100] = "World";
char str3[100];
int i, j;
j=0;
for(i=0;str1[i] != '\0';i++)
{
str3[i] = str1[i];
j++;
}
for(i=0;str2[i] != '\0';i++) {
str3[j] = str2[i];
j++;
}
str3[j] = '\0';
return 0;
}