0% found this document useful (0 votes)
7 views

string programs

Stings program methods

Uploaded by

benhurriderfan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

string programs

Stings program methods

Uploaded by

benhurriderfan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Strings:

a. Write a C program to convert a Roman numeral ranging from I to L to its decimal equivalent
b. Write a C program that converts a number ranging from 1 to 50 to Roman equivalent
c. Write a Cprogram that uses functions to perform the following operations:
To insert a sub-string into a given main string from a given position.
To delete n Characters from a given position in a given string.
d. Write a Cprogram to determine if the given string is a palindrome or not(Spelled same in
both directions with or without a meaning like madam,civic,noon,abcba,etc.)
e. Write a Cprogram that displays the position of a character ch in the string S or –1 if S doesn‘t
contain ch.
f. Write a Cprogram to count the lines,words and characters in a given text.

a) Write a C program that converts a number ranging from 1 to 50 to Roman equivalent


#include<stdio.h>
#include<string.h>
void main()
{
char roman[30];
int deci=0;
int length,i,d[30];
printf("The values of roman I=1\tV=5\tX=10,\t L=50,\tC=100\t D=500\tM=1000");
printf("\nEnter a Roman numeral:");
scanf("%s",roman);
length=strlen(roman);
for(i=0;i<length;i++)
{
switch(roman[i])
{
case 'm':
case 'M': d[i]=1000; break;
case 'd':
case 'D': d[i]= 500; break;
case 'c':
case 'C': d[i]= 100; break;
case 'l':
case 'L': d[i]= 50; break;
case 'x':
case 'X': d[i]= 10; break;;
case 'v':
case 'V': d[i]= 5; break;
case 'i':
case 'I': d[i]= 1;
}
}
for(i=0;i<length;i++)
{
if(i==length-1 || d[i]>=d[i+1])
deci += d[i];
else
deci -= d[i];
}
printf("The Decimal equivalent of Roman numeral %s is %d", roman, deci);

}
b. Write a C program that converts a number ranging from 1 to 50 to Roman equivalent
#include <stdio.h>
int main(void)
{
int num, rem;

printf("Enter a number (1 - 50):");


scanf("%d", &num);
printf("Roman numerals: ");
while(num != 0)
{
if(num>50)
{
printf("Number is Invalid");
}
else if (num == 50) // 50 - l
{
printf("L");
num -= 50;
}
else if (num >= 40) // 40 - xl
{
printf("XL");
num -= 40;
}
else if (num >= 10) // 10 - x
{
printf("X");
num -= 10;
}

else if (num >= 9) // 9 - ix


{
printf("IX");
num -= 9;
}
else if (num >= 5) // 5 - v
{
printf("V");
num -= 5;
}
else if (num >= 4) // 4 - iv
{
printf("IV");
num -= 4;
}
else if (num >= 1) // 1 - i
{
printf("I");
num -= 1;
}
}
return 0;
}

c. Write a Cprogram that uses functions to perform the following operations:


(I)To insert a sub-string into a given main string from a given position.
(II) To delete n Characters from a given position in a given string

#include<stdio.h>
#include<string.h>
void insertstring(char *string,int pos,char *substr);
void deletencharacters(char *string,int pos,int nchar);
int main()
{
int pos,i,j,delpos,nchar;
char string[40],substr[10],newstr[100];
printf("Enter the string::");
gets(string);
printf("Enter the position where u want to insert::");
scanf("%d",&pos);
printf("Enter the substring::");
scanf("%s",substr);
insertstring(string,pos,substr);
printf("enter the position where to delete in %s string",string);
scanf("%d",&delpos);
printf("enter How many characters you want to delete from %d position",delpos);
scanf("%d",&nchar);
deletencharacters(string,delpos,nchar);
}
void insertstring(char string[],int pos,char substr[])
{
char newstr[100];
int i,j;
for(int i=0;i<pos;i++)
{
newstr[i]=string[i];
}
newstr[pos]='\0';
strcat(newstr,substr);
for(i=pos,j=strlen(newstr);i<strlen(string);i++,j++)
newstr[j]=string[i];
puts(newstr);
}
void deletencharacters(char string[],int pos,int nchar)
{
int i;
char newstr[20];
for( i=0;i<pos;i++)
newstr[i]= string[i];
newstr[pos]='\0';
for( i=pos;i<strlen(string);i++)
newstr[i]=string[i+nchar];
newstr[i]='\0';
puts(newstr);
}
Output:

d. Write a Cprogram to determine if the given string is a palindrome or not(Spelled


same in both directions with or without a meaning like madam,civic,noon,abcba,etc.)

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

int main(){
char string1[20];
int i, length;
int flag = 0;

printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
for(i=0;i < length ;i++){
if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}

if (flag) {
printf("%s is not a palindrome", string1);
}
else {
printf("%s is a palindrome", string1);
}
return 0;
}

OUTPUT:

e. Write a Cprogram that displays the position of a character ch in the string S or –1 if S


doesn‘t contain ch.

PROGRAM:
#include <stdio.h>
#include <string.h>
int main()
{
char a, word[50];
int i, freq = 0, flag = 0;
printf("Enter the word: ");
gets(word);
printf("Enter character in the word to find its position and Ocuurence: ");
scanf("%c", &a);
printf("In the word : %s Positions of '%c' Is: ", word,a );
for (i = 0; i<strlen(word); i++)
{
if (word[i] == a)
{
flag = 1;
printf("%d ", i + 1);
freq++;
}
}
if (flag)
{
printf("\nCharacter :'%c' occured for :%d times in the word:%s.\n", a, freq,word);
}
else
{
printf("None\n");
}
return 0;
}
OUTPUT:

f. Write a Cprogram to count the lines,words and characters in a given text.

PROGRAM:

#include<stdio.h>
int main()
{
// declare variables
char str[200];
int line, word, ch;
// initialize count variables with zero
line = word = ch = 0;
// read multiline string
printf("Enter string terminated with ~ :\n");
scanf("%[^~]", str);
// check every character
for(int i=0; str[i]!='\0'; i++)
{
// if it is new line then
// one line and one word completed
if(str[i]=='\n')
{
line++;
word++;
}
// else it is a character
else
{
// if character is space or tab
// then one word is also completed
if(str[i]==' '||str[i]=='\t')
{
word++;
ch++;
}
// it was not '\n', sapace or tab
// it is a normal character
else {
ch++;
}
}
}
// display count values
printf("\nCharacter counts = %d\n", ch);
printf("Word counts = %d\n", word);
printf("Line counts = %d\n", line);
return 0;
}

OUTPUT:

You might also like