0% found this document useful (0 votes)
32 views7 pages

Wa0001 PDF

The document contains multiple C programs that demonstrate various string operations, including finding string length, counting specific characters, deleting vowels, and string manipulation functions like strcpy, strcmp, and strcat. Each program is designed to perform a specific task related to string handling and includes user input and output. The document serves as a practical guide for learning string operations in C programming.

Uploaded by

Sai Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views7 pages

Wa0001 PDF

The document contains multiple C programs that demonstrate various string operations, including finding string length, counting specific characters, deleting vowels, and string manipulation functions like strcpy, strcmp, and strcat. Each program is designed to perform a specific task related to string handling and includes user input and output. The document serves as a practical guide for learning string operations in C programming.

Uploaded by

Sai Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

C program to read a string and find its length using for loop

#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;
}

Write a C program to read a string and count number of occurrences of entered


character.
#include<stdio.h>
int main()
{
char str2[30],ch;
int i,count=0;
printf("Enter the string: ");
gets(str2);
printf("\nEnter character from string that you want to find: ");
scanf("%c",&ch);
for(i=0;str2[i]!='\0';i++)
{
if(ch==str2[i])
{
count++;
}
}
printf("\n'%c' Occurs '%d' Times",ch,count);
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;
}

String Operations without inbuild functions:

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;

printf("Enter any string: ");


gets(text);

printf("Entered string is: %s\n",text);

//convert into upper case


for(i=0;text[i]!='\0';i++)
{
if(text[i]>='a' && text[i]<='z')
text[i]=text[i]-32;
}
printf("String in Upper case is: %s\n",text);

//convert into lower case


for(i=0;text[i]!='\0';i++)
{
if(text[i]>='A' && text[i]<='Z')
text[i]=text[i]+32;
}
printf("String in Lower case is: %s\n",text);

return 0;
}

Strlen operations:
#include <stdio.h>

void main()
{
char str1[50];
int i, l = 0;

printf("\n\nFind the length of a string:\n ");


printf("-------------------------------------\n");

printf("Input a string : ");


scanf("%s", str1);

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


{
l++;
}
printf("The string contains %d number of characters. \n",l);
printf("So, the length of the string %s is : %d\n\n", str1, l);
}

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);

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


s2[i] = s1[i];
}

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;

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


printf("\nSecond string: %s", str2);

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';

// Print the concatenated string


printf("\nConcatenated string: %s", str3);

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy