140 lines
3.2 KiB
C
140 lines
3.2 KiB
C
|
#include <stdio.h>
|
||
|
|
||
|
int string_length(char string[])
|
||
|
{
|
||
|
int i;
|
||
|
for(i=0; string[i]!='\0'; i++);
|
||
|
return i;
|
||
|
}
|
||
|
|
||
|
void string_concat(char str1[], char str2[])
|
||
|
{
|
||
|
int i,j;
|
||
|
char str5[100];
|
||
|
|
||
|
for(i=0;str1[i]!='\0';i++)
|
||
|
str5[i]=str1[i];
|
||
|
|
||
|
str5[i] = '\0';
|
||
|
|
||
|
for(i=string_length(str5),j=0;str2[j]!='\0';i++,j++)
|
||
|
str5[i]=str2[j];
|
||
|
|
||
|
str5[i] = '\0';
|
||
|
|
||
|
printf("Joining Strings 1 and 2, we get: %s\n",str5);
|
||
|
}
|
||
|
|
||
|
void string_comp(char str1[], char str2[])
|
||
|
{
|
||
|
int i;
|
||
|
|
||
|
for(i=0;str1[i]!='\0' || str2[i]!='\0' ;i++){
|
||
|
if(str1[i]>str2[i]){
|
||
|
printf("The first string is greater than the second.\n");
|
||
|
break;
|
||
|
}else if(str1[i]<str2[i]){
|
||
|
printf("The second string is greater than the first.\n");
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void string_insert(char str1[], char str2[], int pos)
|
||
|
{
|
||
|
int i,j;
|
||
|
char str6[100];
|
||
|
|
||
|
if(pos > string_length(str1)){
|
||
|
printf("Invalid Position.");
|
||
|
return;
|
||
|
|
||
|
}else{
|
||
|
|
||
|
for(i=0;i<pos;i++){
|
||
|
str6[i]=str1[i];
|
||
|
}
|
||
|
|
||
|
for(i=pos,j=0;str2[j]!='\0';i++,j++){
|
||
|
str6[i]=str2[j];
|
||
|
}
|
||
|
|
||
|
for(i=pos+string_length(str2),j=pos;str1[j]!='\0';i++,j++){
|
||
|
str6[i]=str1[j];
|
||
|
}
|
||
|
|
||
|
str6[i]='\0';
|
||
|
|
||
|
printf("The new modified string is: %s \n", str6);
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void string_destroy(char str1[], char str3[], int pos)
|
||
|
{
|
||
|
int i, j, str3_len = string_length(str3);
|
||
|
|
||
|
if (pos < 0 || pos >= string_length(str1)) {
|
||
|
printf("Invalid Position.\n");
|
||
|
return;
|
||
|
} else if (str3_len > string_length(str1 + pos)) {
|
||
|
printf("Deletion substring is too long.\n");
|
||
|
return;
|
||
|
} else {
|
||
|
for (i = pos, j = 0; j < str3_len; i++, j++) {
|
||
|
if (str1[i] != str3[j]) {
|
||
|
printf("The substring doesn't match.\n");
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
for (i = pos; str1[i + str3_len] != '\0'; i++) {
|
||
|
str1[i] = str1[i + str3_len];
|
||
|
}
|
||
|
str1[i] = '\0';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void main()
|
||
|
{
|
||
|
int a,b;
|
||
|
char str1[100],str2[100],str3[100];
|
||
|
|
||
|
printf("Enter the first string: ");
|
||
|
gets(str1);
|
||
|
|
||
|
printf("Enter the second string: ");
|
||
|
gets(str2);
|
||
|
|
||
|
printf("The first string is %s. \n",str1);
|
||
|
printf("The second string is %s. \n",str2);
|
||
|
|
||
|
// String Length
|
||
|
|
||
|
printf("The length of String 1 is %d. \n",string_length(str1));
|
||
|
printf("The length of String 2 is %d. \n",string_length(str2));
|
||
|
|
||
|
// String Concatenation
|
||
|
|
||
|
string_concat(str1, str2);
|
||
|
|
||
|
// String Comparison
|
||
|
|
||
|
string_comp(str1, str2);
|
||
|
|
||
|
// String Insertion
|
||
|
|
||
|
printf("Enter the position at which you would like to insert string 2 into string 1: ");
|
||
|
scanf("%d",&a);
|
||
|
|
||
|
string_insert(str1, str2, a);
|
||
|
|
||
|
printf("Enter the position at which you would like to check for deletion substring: ");
|
||
|
scanf("%d",&b);
|
||
|
|
||
|
printf("Enter the part of the string that you want to delete from string 1: ");
|
||
|
getchar(); // Clear the newline character from the input buffer
|
||
|
fgets(str3, sizeof(str3), stdin); // Use fgets instead of gets for safer input
|
||
|
|
||
|
string_destroy(str1, str3, b);
|
||
|
}
|