Saturday, April 30, 2022

Assignment 4: String handling using user defined function and pointers.

 Assignment 4: String handling using user defined function and
pointers.

Set A . Write C programs for the following problems.

1. Write a program to accept a string and find its length using user defined function.(Don’t use pointers)

#include<stdio.h>

/* Function Prototype */

int mystrlen(char str[30]);

/* Main Function */

int main()

{

 char str[30];

 int i, len;

 printf("Enter string:\n");

 gets(str);

 len = mystrlen(str); /* Function Call */

 printf("Length of given string is: %d", len);

 return 0;

}

/* Function Definition */

int mystrlen(char str[30])

{

 int i, len=0;

 for(i=0;str[i]!='\0';i++)

 {

  len++;

 }

 return(len);

}



2. Write a function that takes a string as parameter and returns the same string in upper case(use pointes). Accept this string in main and display converted string in main only.


void stringLwr(char *s);

void stringUpr(char *s);

 int main()

{

    char str[100];

  printf("Enter any string : ");

    scanf("%[^\n]s",str);//read string with spaces

        stringLwr(str);

    printf("String after stringLwr : %s\n",str);

        stringUpr(str);

    printf("String after stringUpr : %s\n",str);

    return 0;

}

 /******** function definition *******/

void stringLwr(char *s)

{

    int i=0;

    while(s[i]!='\0')

    {

        if(s[i]>='A' && s[i]<='Z'){

            s[i]=s[i]+32;

        }

        ++i;

    }

}

 void stringUpr(char *s)

{

    int i=0;

    while(s[i]!='\0')

    {

        if(s[i]>='a' && s[i]<='z'){

            s[i]=s[i]-32;

        }

        ++i;

    }

}



3. Write a function to find reverse of the string and use it in main.

#include <stdio.h>  

#include <string.h>  

void revstr(char *str1)  

{  

   

    static int i, len, temp;  

    len = strlen(str1); 

      if (i < len/2){  

    

        temp = str1[i];  

        str1[i] = str1[len - i - 1];  

        str1[len - i - 1] = temp;  

        i++;  

        revstr(str1); 

    }  

}  

 

    int main()  

    {  

        char str1[50]; // size of char string  

        printf (" Enter the string: ");  

        gets(str1);           

        printf (" \n Before reversing the string: %s \n", str1);  

        revstr(str1);  

        printf (" After reversing the string: %s", str1);  

    }  


Set B. Write C programs for the following problems.
1. Write a function to compare two strings. Write another function to reverse the string.In main function a string and check whether it is palindrome or not using above functions. (Hint: A palindrome string is a string which reads same in forward as well as backward direction for example: madam, nitin, etc.)


#include<stdio.h>

int mystrcmp(char str1[40], char str2[40]);

char* reverse(char* str);

int main()

{

 char str[40];

 int d;

char *rev;

 printf("Enter first string:\n");

 gets(str);

  rev = reverse(str);

 d = mystrcmp(str, rev);

 if(d==0)

 {

  printf("Given string is pallindrome .");

   }

 else

 {

  printf("Given string is not pallindrome.");

 }

 return 0;

}

char* reverse(char *str) {                     

static int i = 0;

static char rev[100];

if(*str)

{

reverse(str+1);

rev[i++] = *str;

}

return rev;

}

/* Function Definition */

int mystrcmp(char str1[40], char str2[40])

{

 int d,i, len1=0, len2=0, flag=0;

 /* Finding length of first string */

 

 for(i=0; str1[i]!='\0'; i++)

 {

  len1++;

 }

  /* Finding length of first string */

 for(i=0; str2[i]!='\0'; i++)

 {

  len2++;

 }

 if(len1!=len2)

 {

  return(1);

 }

 else

 {

  for(i=0;i< len1;i++)

  {

   if(str1[i]!=str2[i])

   {

    flag=1;

    break;

   }

  }

    if(flag==0)

  {

   return(0);

  }

  else

  {

   return(1);

  }

 }

}


1. Write a menu driven program to perform the following operations on one string using pointers.

i. Length

ii. Reverse

 iii, Convert to uppercase

iv. Convert to lowercase (Write a separate function for each operation)

 

#include<stdio.h>

/* Function Prototype */

int mystrlen(char str[30]);

/* Main Function */

int main()

{

char str[30];

int i, len, ch;

printf("Enter string:\n");

gets(str);

printf("\n MENU \n1. Length \n 2. Reverse \n 3. Convert to uppercase \n 4. Convert to lowercase \n ");

printf("\n Enter your choice ");

scanf("%d", &ch );

switch (ch)

{

case 1: len = mystrlen(str); /* Function Call */

printf("Length of given string is: %d", len);

break;

 

case 2: printf (" \n Before reversing the string: %s \n", str1);

 revstr(str1);

 printf (" After reversing the string: %s", str1);

break;

case 3 :stringUpr(str);

 printf("String after stringUpr : %s\n",str);

break;

 

case 4:stringLwr(str);

 printf("String after stringLwr : %s\n",str);   

break;

}

return 0;

}

 

/* Function Definition */

int mystrlen(char str[30])

{

int i, len=0;

for(i=0;str[i]!='\0';i++)

{

 len++;

}

return(len);

} 

 void revstr(char *str1)

{

 static int i, len, temp;

 len = strlen(str1);

 if (i < len/2)

{

 temp = str1[i];

 str1[i] = str1[len - i - 1];

 str1[len - i - 1] = temp;

 i++;

 revstr(str1);

 }

} 

void stringLwr(char *s)

{

 int i=0;

 while(s[i]!='\0')

 {

 if(s[i]>='A' && s[i]<='Z'){

 s[i]=s[i]+32;

 }

 ++i;

 }

}

void stringUpr(char *s)

{

 int i=0;

 while(s[i]!='\0')

 {

 if(s[i]>='a' && s[i]<='z'){

 s[i]=s[i]-32;

 }

 ++i;

 }

} 

 

 

2. Write a menu driven program to perform the following operations on two strings using pointers.

i. Copy

 ii.Concatenation

iii. Compare

(Write a separate function for each operation, Copy and Concatenate should return the resulting string)

 

 

#include <stdio.h>  

int stringcompare(char*,char*);  

int main()  

{  

int ch;

  char str1[20]; // declaration of char array  

  char str2[20]; // declaration of char array  

  printf("Enter the first string : ");  

  scanf("%s",str1);  

  printf("\nEnter the second string : ");  

  scanf("%s",str2);  

 

printf("\n MENU \n1. Copy  \n 2. Concatenate  \n 3.Compare  \n ");

printf("\n Enter your choice ");

scanf("%d", &ch );

switch (ch)

{

case 1: copy_string(str1,str2)

break;

case 2 : concatenateString(str1, str2);

break;

case 3: int compare=stringcompare(str1,str2); // calling stringcompare() function.  

  if(compare==0)  

  printf("strings are equal");  

 else  

 printf("strings are not equal");  

break;

}

return 0;  

}  

// Comparing both the strings using pointers  

int stringcompare(char *a,char *b)  

{  

   int flag=0;  

    while(*a!='\0' && *b!='\0')  // while loop  

    {  

        if(*a!=*b)  

        {  

            flag=1;  

        }  

        a++;  

        b++;  

    }  

      

    if(flag==0)  

    return 0;  

    else  

    return 1;  

}  

 

void copy_string(char *a, char *b)

{

    while(*b)

    {

        *a = *b;        

        b++;        

        a++;

    }    

    *a = '\0';

}

char* concatenateString(char *a, char *b){

    if(NULL == a || NULL == b){

        return NULL;

    }

    int index = 0, length = strlen(a);

    while(b[index] != '\0'){

        a[length] = b[index];

        length++;

        index++;

    }

    a[length] = '\0';

    return a;

}

 

Set B. Write C programs for the following problems.

1. Write a program that accepts n words and outputs them in dictionary order. (Use array of pointers and dynamically allocate memory for each word)

#include<stdio.h>

#include <string.h>

int main()

{

 int i, j;

 char str[10][50], temp[50];

 printf("\nEnter 10 words:: \n");

 for(i=0; i<10; ++i)

 scanf("%s[^\n]",str[i]);

 for(i=0; i<9; ++i)

 for(j=i+1; j<10 ; ++j)

 {

 if(strcmp(str[i], str[j])>0)

 {

 strcpy(temp, str[i]);

 strcpy(str[i], str[j]);

 strcpy(str[j], temp);

 }

 }

printf("\nIn lexicographical order: \n");

 for(i=0; i<10; ++i)

 {

 puts(str[i]);

 }

 return 0;

} 

2. Write a function which returns the substring of a given string using pointers. Use this function in main. Function prototype: char * substring(char *str, int start, int end);

 

#include <stdio.h>

#include <stdlib.h>

char* substring(char*, int, int);

int main()

{

   char string[100], *p;

   int position, length;

   printf("Input a string\n");

   gets(string);

   printf("Enter the position and length of substring\n");

   scanf("%d%d", &position, &length);

   p = substring(string, position, length);

   printf("Required substring is \"%s\"\n", p);

   free(p);

   return 0;

}

 

/*C substring function: It returns a pointer to the substring */

char *substring(char *string, int position, int length)

{

   char *p;

   int c;

   p = malloc(length+1);

   if (p == NULL)

   {

      printf("Unable to allocate memory.\n");

      exit(1);

   }

 

   for (c = 0; c < length; c++)

   {

      *(p+c) = *(string+position-1);      

      string++;  

   }

   *(p+c) = '\0';

   return p;

}

 

Set C. Write programs to solve the following problems

1. Write a function, which displays a given number in words. For Example:

129 One Hundred Twenty Nine

2019 Two Thousand Nineteen

 

//This program can be used to convert a number to words

 

#include <stdio.h>

#include <string.h>

void convert(char *num)

{

int len = strlen(num); // find no of digit

/* no number */

if (len == 0) {

fprintf(stderr, “empty string\n”);

return;

}

 

char *single_digits[] = { “zero”, “one”, “two”, “three”, “four”,”five”,”six”, “seven”, “eight”, “nine”};

 

char *two_digits[] = {“”, “ten”, “eleven”, “twelve”, “thirteen”, “fourteen”,”fifteen”, “sixteen”,”seventeen”, “eighteen”, “nineteen”};

 

char *tens_multiple[] = {“”, “”, “twenty”, “thirty”, “forty”, “fifty”,“sixty”, “seventy”, “eighty”, “ninety”};

 

char *tens_power[] = {“hundred”, “thousand”};

/* single number*/

if (len == 1) {

printf(“%s\n”, single_digits[*num – ‘0’]);

return;

}

 

while (*num != ‘\0’) {

if (len >= 3) {

if (*num -‘0’ != 0) {

printf(“%s “, single_digits[*num – ‘0’]);

printf(“%s “, tens_power[len-3]); // here len can be 3 or 4

}

–len;

}

 

/* Code path for last 2 digits */

else {

if (*num == ‘1’) {

int sum = *num – ‘0’ + *(num + 1)- ‘0’;

printf(“%s\n”, two_digits[sum]);

return;

}

else if (*num == ‘2’ && *(num + 1) == ‘0’) {

printf(“twenty\n”);

return;

}

 

/* number range 21 to 99 */

else {

int i = *num – ‘0’;

printf(“%s “, i? tens_multiple[i]: “”);

++num;

if (*num != ‘0’)

printf(“%s “, single_digits[*num – ‘0’]);

}

}

++num;

}

}

 

int main(void)

{

char a[10];

printf(“\nEnter the number : “);

scanf(“%s”,a);

printf(“\nThe number in word is “);

convert(a);

return 0;

}

No comments:

Post a Comment