Question

In C write a menu driven program to write your own string handling functions. Don’t use...

In C write a menu driven program to write your own string handling functions. Don’t use
static arrays, ONLY dynamic memory allocation is allowed.

1. Convert a string to integer.
2. Concatenate two strings
3. Copy string to another string
4. Compare two strings
5. String length
6. Quit

Homework Answers

Answer #1

#include <stdio.h>
#include <string.h>

void convertToString() // convert string to int
{
char str[100];
int x;
printf("Please enter the string: ");
scanf("%s", str);
sscanf(str, "%d", &x);
printf("\nThe value of x : %d", x);
}

void concatenate() // concatanate two strings
{
char str1[100], str2[100];
printf("Please enter the two strings: ");
scanf("%s", str1);
scanf("%s", str2);
strcat(str1, str2);
printf("String obtained on concatenation: %s\n", str1);
}

void copyStrings() // copies one string to another string
{
char str1[100], str2[100];
printf("Please enter the string: ");
scanf("%s", str1);
strcpy(str2, str1);
printf("After copying: %s\n", str2);
}

void comparing() // comparing two string if they are equal or not
{
char str1[100], str2[100];
printf("Please enter the two strings: ");
scanf("%s", str1);
scanf("%s", str2);
if(strcmp(str1, str2) == 0)
{
printf("Hence both are equal");
}
else
{
printf("Both are not equal");
}
}

void stringLength() // prints string length
{
char str1[100];
printf("Please enter the two strings: ");
scanf("%s", str1);
printf("String length is : %d", strlen(str1));
}

int main()
{
do{
int n;
char str[1000];
int x;
printf("\n");
printf("1. Convert string to integer\n");
printf("2. Concatenate two strings\n");
printf("3. Copy string to another string\n");
printf("4. Compare two strings\n");
printf("5. String Length\n");
printf("6. Quit\n");
printf("please enter your choice: ");
scanf("%d",&n);
printf("\n");
switch(n)
{
case 1: convertToString(); printf("\n"); break;
  
case 2: concatenate(); printf("\n"); break;
  
case 3: copyStrings(); printf("\n"); break;
  
case 4: comparing(); printf("\n"); break;
  
case 5: stringLength(); printf("\n"); break;
  
default : exit(0);
  
  
}
}while(1);

return 0;
}


OUTPUT :

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two...
Build two arrays[ ] (Integer and String) and convert them to two ArrayLists and write two overloaded generic static search method to find the index locations of a specified value. One of the search methods applies to the array type while the other (overloaded) search method applies to the collection type. Implement the following generic linear search method and write a client program to display results: (Here is the header) public static <E extends Comparable<E>> int search(E[] list, E key)...
Write a C++ program which consists of several functions besides the main() function. 1)   The main()...
Write a C++ program which consists of several functions besides the main() function. 1)   The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. 2)   A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. 3)   A value-returning function called Power(int a,...
Objectives: ⦁   Declare and initialize null-terminated string ⦁   Apply indirect address ⦁   Write loop ⦁   Apply...
Objectives: ⦁   Declare and initialize null-terminated string ⦁   Apply indirect address ⦁   Write loop ⦁   Apply Irvine.inc library functions to display a string Problem Description: Write a program with a loop and indirect address that copies a string from source to target. Revising the character order in the process. Use the following variables: source BYTE “This is the string that will be reversed”, 0 target BYTE SIZEOF source DUP(‘#’) You may refer to the Programming Exercise #7 on Page 138...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
In this assignment you will write a program that compares the relative strengths of two earthquakes,...
In this assignment you will write a program that compares the relative strengths of two earthquakes, given their magnitudes using the moment magnitude scale. Earthquakes The amount of energy released during an earthquake -- corresponding to the amount of shaking -- is measured using the "moment magnitude scale". We can compare the relative strength of two earthquakes given the magnitudes m1 and m2 using this formula: f=10^1.5(m1−m2) If m1>m2, the resulting value f tells us how many times stronger m1...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT