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

C Programs for String Manipulation Tasks

The document contains multiple C programs that perform various string operations, including searching for a character, checking for palindromes, inserting and deleting substrings, and converting Roman numerals to decimal. Each program is structured to take user input and process it accordingly, demonstrating fundamental string manipulation techniques in C. The document serves as a practical guide for implementing these common string operations.

Uploaded by

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

C Programs for String Manipulation Tasks

The document contains multiple C programs that perform various string operations, including searching for a character, checking for palindromes, inserting and deleting substrings, and converting Roman numerals to decimal. Each program is structured to take user input and process it accordingly, demonstrating fundamental string manipulation techniques in C. The document serves as a practical guide for implementing these common string operations.

Uploaded by

maniteja2707
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

C Program to search a character in a given string

#include <stdio.h>

int main()
{
char str[100],ch;
int index=-1;
int NT;

printf("\nenter the no of testcases: ");


scanf("%d", &NT);

while (NT--)
{

printf("\nenter the string, character to search: ");


scanf("%s", str);
scanf(" %c", &ch);

int i=0;
while(str[i]!='\0')
{
if (str[i] == ch)
{
index = i;
break;
}
i++;
}
printf("%d\n", index);
}
return 0;
}
C Program to check whether a string is Palindrome or not.

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

int palindrome(char[]);

int main()
{
char str[30];
puts("enter the string");
scanf("%s",str);
int status= palindrome(str);
if(status==0)
{
printf("\nstring is palindrome");
}
else
{
printf("\nstring is not a palindrome");
}
return 0;
}

int palindrome(char str[30])


{
int left,right;

left=0;
right=strlen(str)-1;

while(left<right)
{
if(str[left] != str[right])
{
return -1;
}
left++;
right--;
}
return 0;
}
C Program for inserting sub string in to a main string from a given
position

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

int main()
{
char str[100], sub[100], res[200];
int position, NT;

printf("\nenter the no of test cases: ");


scanf("%d", &NT);

while (NT--)
{
printf("\nenter the string, sub string and position: ");
scanf("%s %s", str, sub);
scanf("%d", &position);

strncpy(res, str, position - 1);


res[position - 1] = '\0';

strcat(res, sub);
strcat(res, str + (position - 1));

printf("%s\n", res);
}

return 0;
}
C Program to delete n characters from a given position in a given
string

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

int main()
{

char str[100], res[100];


int position, n, NT;

printf("\nenter the number of testcases: ");


scanf("%d", &NT);

while (NT--)
{
printf("\nenter the string, position, no of characters to delete: ");
scanf("%s", str);
scanf("%d %d", &position, &n);

int length = strlen(str);

if (position < 1 || position > length)


{
printf("%s\n", str);
continue;
}

int i,j=0;
for(i=0;str[i]!='\0';i++)
{
if(i>=(position-1) && i<(position-1)+n)
{
continue;
}
res[j]=str[i];
j++;
}
res[j]='\0';

printf("%s\n", res);
}

return 0;
}

C Program to convert Roman number into Decimal number

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

int finddecimal(char ch)


{
switch (ch)
{
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
default: return 0;
}
}

int main()
{
int NT, val, nextval;
char str[20];

printf("\nenter no of test cases: ");


scanf("%d", &NT);

while(NT--)
{
scanf("%s", str);
int decimal = 0;
int length = strlen(str);
for (int i = 0; i < length; i++)
{
val = finddecimal(str[i]);
nextval = finddecimal(str[i+1]);
if (val < nextval)
{
decimal = decimal-val;
}
else
{
decimal = decimal+val;
}
}
printf("\nequivalent decimal number is %d\n", decimal);

return 0;
}
C program for inserting sub string in to a main string from a given
position ( without using string functions)

#include<stdio.h>

void insert_string(char[],char[],char[],int);

int main()
{
char str[30],sub[30],res[30];
int pos;

printf("\nenter the main string:");


scanf(“%s”,str);
printf("\nenter the sub string:");
scanf(“%s”,sub);

printf("\nenter the position: ");


scanf("%d",&pos);

insert_string(str,sub,res,pos);
printf("\nresultant string is: %s",res);
return 0;
}

void insert_string(char str[30],char sub[30],char res[30],int pos)


{
int i,j,k;
for(i=0;i<pos;i++)
{
res[i]=str[i];
}
for(j=0;sub[j]!='\0';j++)
{
res[i]=sub[j];
i++;
}
for(k=pos;str[k]!='\0';k++)
{
res[i]=str[k];
i++;
}
res[i]='\0';
}

You might also like