10. Char name[20]=“SDS”;
‘S’ ‘D’ ‘S’ ‘0’ blank blank
1. Initialize len =0;
2. Perform following steps until len reaches to ‘0’
i) otherwise increment value of len by1.
3. return len.
len
14. ‘S’ ‘D’ ‘S’ ‘R’ ‘K’ ‘0’ blank
i j
s
k
Char name[20]=“SDSRK”;
15. ‘K’ ‘D’ ‘S’ ‘r’ ‘S’ ‘0’ blank
i j
D R
Char name[20]=“SDSRK”;
16. ‘K’ ‘R’ ‘S’ ‘D’ ‘S’ ‘0’ blank
ij
1. Find length of string
2. Initialize i=0 and j=length-1.
3. Perform following steps till i less than length/2
i) exchange the element at ith and jth position.
ii) increment i by 1
iii) decrement j by 1
Char name[20]=“SDSRK”;
23. 1. Find length of string1.
2. Find length of string2.
3. Initialize i=length of string1 and j to 0.
4. Perform following steps till j not reaches to
length of string2
i) copy jth element of string2 in ith element of
string1.
ii) increment i and j by 1.
5. Store ‘0’ to ith element of string1.
30. 1. Find length of string.
2. Initialize i = 0.
3. Perform following steps till i is not equal to length of string.
a) check whether ith element of string is lowercase or not.
i) if it is lowercase then subtract 32 from ASCII value.
b) increment i by 1.
33. 1. Initialize i=0.
2. Perform following steps till any of the strings
get ‘0’
i) check ith element of both the strings.
ii) if they are not same then return the difference
of ASCII value
iii) else increase the value of i by 1.
3. If both strings are same, return 0.
34. int compare(char str1[],char str2[])
{
int i;
for(i=0;str1[i]!='0'||str2[i]!='0';i++)
{
if (str1[i]!=str2[i])
return (str1[i]-str2[i]);
}
return 0;
}
36. 1. Find length of string1.
2. Find length of string2.
3. Initialize i = 0, j=0.
4. Perform following steps till i is less or equal to difference of
strings.
a) check whether ith element of string1 is equal to jth element of
string2.
b) if they are same then check other elements of string2 with
string1 untill length of string2
c) increment i by 1.
5. If substring is not found then return 0.
37. int substring(char str1[],char str2[])
{
int i,j,k,len1,len2;
for(len1=0;str1[len1]!='0';len1++);
for(len2=0;str2[len2]!='0';len2++);
for(i=0,j=0;i<=len1-len2;i++)
{ if (str1[i]==str2[j])
{
for(j=0,k=i;j<len2;k++,j++)
{ if(str1[k]!=str2[j])
return 0;
}
return i;
}
}
return 0;
}
39. 1. Find length of string.
2. Initialize i to 0 and j to length-1.
3. Perform following steps till i is less than half of string.
a) check whether ith element and jth element are same or not.
b) if not than return ASCII difference
c) increment i by 1 and decrement j by 1.
5. return 0.
40. int palindrome(char str[])
{
int i,j,len;
for(len=0;str[len]!='0';len++);
i=0;j=len;
while(i<len/2)
{
if(str[i]!=str[j])
return str[i]-str[j];
i++; j--;
}
return 0;
}
41. int palindrome(char str[]) {…………..}
void main()
{
char str[20];
int s;
clrscr();
gets(str);
s=palindrome(str);
printf("nString is %s palindrome.",(s==0)?("a"):(s));
getch();
}
42. HW
Write an algorithm and function that gives ascii difference of 2 characters.
Ie. String is ‘abcd’
Output:
a-b =ASCII (1)
b-c=ASCII (1)
c-d=ASCII (1)