Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views8 pages

String

The document contains multiple C programming examples demonstrating various operations such as displaying command line arguments in reverse order, calculating string length, summing two numbers from command line arguments, performing string operations (copying, comparing, concatenating), counting vowels and consonants, and finding the maximum of three integers. Each example is accompanied by code snippets that illustrate the implementation of these functionalities. Additionally, there is a menu-driven program to perform string operations using standard library functions.

Uploaded by

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

String

The document contains multiple C programming examples demonstrating various operations such as displaying command line arguments in reverse order, calculating string length, summing two numbers from command line arguments, performing string operations (copying, comparing, concatenating), counting vowels and consonants, and finding the maximum of three integers. Each example is accompanied by code snippets that illustrate the implementation of these functionalities. Additionally, there is a menu-driven program to perform string operations using standard library functions.

Uploaded by

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

1) Write a program to display the command line arguments in reverse

order.

#include<stdio.h>
#include<stdlib.h>
int main(int argc,char * argv[])
{
int i;
for(i=argc-1;i>0;i--)
printf("\n Argument [%d] is : %s",i,argv[i]);
return 0;
}

2) Write a program to calculate length of string using standard library


function.

#include<stdio.h>
#include<string.h>

int main()
{
char str[100];
int len;

printf("Enter the String : ");


scanf("%s",str);

len = strlen(str);

printf("Length of the given string is %d", len);


return(0);
}
3) Write a program to calculate sum of two numbers. Pass the numbers as
command line arguments.
#include <stdio.h>
int main(int argc, char *argv[])
{
int a,b,sum;
if(argc!=3)
{
printf("insufficient arguments \n");
return -1;
}

a = atoi(argv[1]);
b = atoi(argv[2]);

sum = a+b;

printf("Sum of %d, %d is: %d\n",a,b,sum);

return 0;
}
4) Write a program to perform the following operations on two strings
using standard library functions:
a. Copy b. Compare

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
char s1[50],s2[50],s3[50];
printf("\n Enter String: ");
scanf("%s",s1);
strcpy(s3,s1);
printf("\n Copied String is : %s",s3);
printf("\n Enter String2 : ");
scanf("%s",s2);
if(strcmp(s1,s2)==0)
printf("\n Entered strings are equal...");
else
printf("\n Entered strings are different..");
return 0;
}
5) Write a C program to count the number of vowels and consonants in a
string.

#include <stdio.h>
int main()
{

int i, vCount = 0, cCount = 0;


char str[ ] = "This is a really simple sentence";
for(i = 0; i < strlen(str) ; i++)
{
str[i] = tolower(str[i]);
if(str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u')
{
vCount++;
}
else
if(str[i] >= 'a' && str[i] <= 'z')
{
cCount++ ;
}
}
printf("Number of vowels : %d\n", vCount);
printf("Number of consonant : %d", cCount);
return 0;
}
6) Write a C program to concatenate two strings using standard library
function.

#include <stdio.h>
#include <string.h>
int main()
{
char destination[] = "Hello ";
char source[] = "World!";
strcat(destination,source);
printf("Concatenated String: %s\n", destination);
return 0;
}

7) Write a C program to accept three integers as command line arguments


and find the maximum of the three.

#include<stdio.h>

void main(int argc , char * argv[])


{
int i,sum=0;
float avg;
int max;
int min;
if(argc!=4)
{
printf("you have forgot to type numbers.");
exit(1);
}

for(i=1;i<argc;i++)
{
sum = sum + atoi(argv[i]);
avg=sum/3;
}
printf("the average is%f",avg);

max=argv[1];
min=argv[1];
for(i=1;i<argc;i++)
{
if(atoi(argv[i])>max)
{
max=atoi(argv[i]);
}
}
if(atoi(argv[i])<min)
{
min=atoi(argv[i]);
}
printf("\n the maximum number is %d",max);
printf("\n the minimum number is %d",min);

8) Write a menu-driven program in C to perform the following


operations on string using standard library functions.

i) Calculate Length of String


ii) Reverse a given String
iii) Concatenation of one string to another
iv) Copy one String into another
v) Compare two Strings

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char str1[20],str2[20];
int ch,i,j;

printf("\t MENU");
printf("1:Find Length of String");
printf("\n2:Find Reverse of String");
printf("\n3:Concatenate Strings");
printf("\n5:Copy String ");
printf("\n5:Compare Strings");
printf("\n6:Exit");

printf("\nEnter your choice: ");


scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter String: ");
scanf("%s",str1);
i=strlen(str1);
printf("Length of String : %d\n\n",i);
break;
case 2:
printf("Enter String: ");
scanf("%s",str1);
//strrev(str1);
printf("Reverse string : %s\n\n",str1);
break;
case 3:
printf("\nEnter First String: ");
scanf("%s",str1);
printf("Enter Second string: ");
scanf("%s",str2);
strcat(str1,str2);
printf("String After Concatenation : %s\n\n",str1);
break;
case 4:
printf("Enter a String1: ");
scanf("%s",str1);
printf("Enter a String2: ");
scanf("%s",str2);
printf("\nString Before
Copied:\nString1=\"%s\",String2=\"%s\"\n",str1,str2);
strcpy(str2,str1);
printf("-----------------------------------------------\n");
printf("\"We are copying string String1 to String2\" \n");
printf("-----------------------------------------------\n");
printf("String After Copied:\nString1=\"%s\",
String2=\"%s\"\n\n",str1,str2);
break;
case 5:
printf("Enter First String: ");
scanf("%s",str1);
printf("Enter Second String: ");
scanf("%s",str2);
j=strcmp(str1,str2);
if(j==0)
{
printf("Strings are Same\n\n");
}
else
{
printf("Strings are Not Same\n\n");
}
break;
case 6:
exit(0);
break;
default:
printf("Invalid Input. Please Enter valid Input.\n\n ");
}
return 0;
}

You might also like