Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Saturday, May 21, 2011

C Program to produce a substring from given string

/*Write a program to copy m consecutive characters from String S1
beginning at position n into another string S2.*/

int main()
{
int m,n,i;
char s2[40],s1[40];
printf("Enter the string 1");
scanf("%s",&s1);
printf("\nHow many characters you want to copy");
scanf("%d",&n);
printf("From which position you want it to be copied");
scanf("%d",&m);
for(i=m-1;i {

s2[i-m-1]=s1[i];
}

printf("\nThe string s2 is %d",puts(s2));
system("pause");
return 0;
}

C Program to perform operation on Strings (Array of Character)

Read a line of text into a character array and perform the following
operation:
a. Count the number of Upper case , Lower case, digits and
special characters.
b. Convert Upper case to Lower case and vice versa without
using string library functions.
c. Count the number of words, lines and characters in a given
text.


main()
{
char a[40];
int i=0,lc=0,uc=0,w=0,le=0,d=0,s=0;
printf("Enter string");
gets(a);

while(a[i]!='\0')
{
if(a[i]>='a' && a[i]<='z')
{
lc++;
a[i]=a[i]-32;
le++;
}
else if (a[i]>='A' && a[i]<='Z')
{
uc++;
a[i]=a[i]+32;
le++;
}
else if (a[i]==' ')
w++;
else if(a[i]=='.')
s++;
else if (a[i]>='0' && a[i]<='9' )
d++;

i++;
}
printf("%s\n",a);
printf("Upper case %d\n",uc);
printf("Lower case %d\n",lc);
printf("Digits %d\n",d);
printf("Sentences/lines %d\n",s);
printf("Number of words %d\n",++w);
getch();
}