Question

**C** Write a C function that inputs a pointer to a string and a pointer to...

**C**

Write a C function that inputs a pointer to a string and a pointer to a buffer. The function then scans through the string removing leading and trailing whitespace and replacing any run of whitespace within the string with a single space. Your function should follow this prototype:

void tighten(char *oldstring, char* newstring, int length);

The first argument is a pointer to a null-terminated string sitting somewhere in memory. The second argument is a pointer to the first char of a buffer of size length. The function will copy a modified version of the old string into the buffer.

For example, if the original string is “ Be true to the dreams of thy youth!! ”

the new string in the buffer would be “Be true to the dreams of thy youth!!”

Of course, the new string will be terminated with a null. Don't make any assumptions about what is in the buffer to start with. It may have been used several times before. Stop copying characters from the old string to the new string when the new string is full (has length-1 characters). Be sure to terminate the new string with null.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

void removeSpaces(char *str, char *newstring, int length)
{
int count = 0 , i;

for (i = 0; str[i]; i++)
if (str[i] != ' ')
newstring[count++] = str[i];
  
newstring[count] = '\0';
}

int main()
{
int length;
char str[] = "Be true to the dreams of thy youth!!";
length = strlen(str);
char new[length];
removeSpaces(str, new, length);
printf("%s",new);
return 0;
}


==================
See Output


* New Project-201 70326日ロ+ « 。もCompile | | Execute | | > Share Code main.c × root 13 14 15 16 int mainO 17-5 18 19 20 21 news

Thanks, let me know if there is any concern.



EDIT: After Correction

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

void removeSpaces(char *str, char *newstring, int length)
{
int count = 0 , i;

for (i = 0;i<length-1; i++)
if (str[i] != ' ')
   newstring[count++] = str[i];
else if (str[i] == ' ' && str[i+1] != ' ')
   newstring[count++] = str[i];

newstring[count] = '\0';
}

int main()
{
int length;
char str[] = "Be true to the dreams of thy youth!!";
length = strlen(str);
char new1[length];
removeSpaces(str, new1, length);
printf("%s",new1);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
**C** Write a C function that inputs a pointer to a string and a pointer to...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. String Length Write a function that returns an integer and accepts a pointer to a...

    1. String Length Write a function that returns an integer and accepts a pointer to a C-string as an argument. The function should count the number of characters in the string and return that number. Demonstrate the function in a simple program that asks the user to input a string, passes it to the function, and then displays the function’s return value. c++

  • Write in C language 5. [8] Write a function with prototype » void string_copy(const char source[],...

    Write in C language 5. [8] Write a function with prototype » void string_copy(const char source[], char destination[], int n); This function copies string source to string destination. Parameter n represents the size of array destination. If the latter array is not sufficiently large to hold the whole source string then only the prefix of the string which has room in the latter array should be copied. Note that after copying, the null character should also be included to mark...

  • Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX...

    Part1. Write a C program contains the following declarations: char customer_name[N_CUSTOMERS][MAX_NAME_LENGTH]; int customer_number[N_CUSTOMERS] A program uses a text file that contains the following data on each line: The customer number is an int, and the first and last names are alphabetic strings that contain no whitespace. The last and first names themselves are however separated by whitespace. Write a C function with the following prototype: void read_customer (char name[][MAX_NAME_LENGTH], int number[], int position, FILE *cust_file) Your function should read a...

  • In the space below, write a C function definition for a function named StrLower, which will...

    In the space below, write a C function definition for a function named StrLower, which will receive one parameter, a pointer to a null-terminated string (i.e., pointer to char), convert each character in the string to lowercase (using the tolower function from <ctype.h>), and return nothing to the caller. Inside the function definition, you may use a for loop or a while loop. If you use any variables other than the incoming parameter, you must define them locally in the...

  • IN C language Write a C program that prompts the user to enter a line of...

    IN C language Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr andcreadLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate...

  • Write a function that accepts a pointer to a C-string as its argument. The function should...

    Write a function that accepts a pointer to a C-string as its argument. The function should count the number of times the character 'w’ occurs in the argument and return that number.

  • 2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics...

    2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop....

  • Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and...

    Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop. Remember...

  • Objectives: Use strings and string library functions. Write a program that asks the user to enter...

    Objectives: Use strings and string library functions. Write a program that asks the user to enter a string and output the string in all uppercase letters. The program should then display the number of white space characters in the string. You program should run continuously until the user enters an empty string. The program must use the following two functions: A function called count_spaces that counts the number of white spaces inside a string. int count_space(char str[]); which tell you...

  • 2. (50 marks) A string in C++ is simply an array of characters with the null...

    2. (50 marks) A string in C++ is simply an array of characters with the null character(\0) used to mark the end of the string. C++ provides a set of string handling function in <string.h> as well as I/O functions in <iostream>. With the addition of the STL (Standard Template Library), C++ now provides a string class. But for this assignment, you are to develop your own string class. This will give you a chance to develop and work with...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT