This simple program shows you how to find the string length, copy a string and concatenate two strings without using built-in string functions.
//Program to manipulate strings without string functions
#include<stdio.h>
#include<conio.h>
main()
{
char *str1,*str2;
int i,j, choice;
clrscr();
printf("1. String length\n");
printf("2. String copy\n");
printf("3. String Concat\n");
printf("Enter choice\n");
scanf("%d",&choice);
switch (choice)
{
case 1: printf("\nEnter string\n");
fflush(stdin);
gets(str1);
for(i=0;str1[i]!='\0';i++);
printf("The length of the string \"%s\" is %d",str1,i);
break;
case 2: printf("\nEnter string to be copied\n");
fflush(stdin);
gets(str1);
for(i=0;str1[i]!='\0';i++)
str2[i]=str1[i];
str2[i]='\0';
printf("String1 = %s",str1);
printf("\nThe copied string str2 = %s", str2);
break;
case 3: printf("\nEnter string1 and string2\n");
fflush(stdin);
gets(str1);
gets(str2);
for(i=0;str1[i]!='\0';i++);
for(j=0;str2[j]!='\0';j++)
str1[i++]=str2[j];
str1[i]='\0';
printf("\nThe concatenated string is %s",str1);
break;
default: printf("\nWrong choice\n");
}
getch();
}
/*
OUTPUT
--------
1. String length
2. String copy
3. String Concat
Enter choice
3
Enter string1 and string2
good
morning
The concatenated string is goodmorning
1. String length
2. String copy
3. String Concat
Enter choice
1
Enter string
welcome
The length of the string "welcome" is 7
1. String length
2. String copy
3. String Concat
Enter choice
2
Enter string to be copied
welcome
String1 = welcome
The copied string str2 = welcome
*/
//Program to manipulate strings without string functions
#include<stdio.h>
#include<conio.h>
main()
{
char *str1,*str2;
int i,j, choice;
clrscr();
printf("1. String length\n");
printf("2. String copy\n");
printf("3. String Concat\n");
printf("Enter choice\n");
scanf("%d",&choice);
switch (choice)
{
case 1: printf("\nEnter string\n");
fflush(stdin);
gets(str1);
for(i=0;str1[i]!='\0';i++);
printf("The length of the string \"%s\" is %d",str1,i);
break;
case 2: printf("\nEnter string to be copied\n");
fflush(stdin);
gets(str1);
for(i=0;str1[i]!='\0';i++)
str2[i]=str1[i];
str2[i]='\0';
printf("String1 = %s",str1);
printf("\nThe copied string str2 = %s", str2);
break;
case 3: printf("\nEnter string1 and string2\n");
fflush(stdin);
gets(str1);
gets(str2);
for(i=0;str1[i]!='\0';i++);
for(j=0;str2[j]!='\0';j++)
str1[i++]=str2[j];
str1[i]='\0';
printf("\nThe concatenated string is %s",str1);
break;
default: printf("\nWrong choice\n");
}
getch();
}
/*
OUTPUT
--------
1. String length
2. String copy
3. String Concat
Enter choice
3
Enter string1 and string2
good
morning
The concatenated string is goodmorning
1. String length
2. String copy
3. String Concat
Enter choice
1
Enter string
welcome
The length of the string "welcome" is 7
1. String length
2. String copy
3. String Concat
Enter choice
2
Enter string to be copied
welcome
String1 = welcome
The copied string str2 = welcome
*/
No comments:
Post a Comment