Question

3. Create a program which allows the user to swap individual words in an input string....

3. Create a program which allows the user to swap individual words in an input string. Given a string of input, your program should count and store the number of words in a 2D array. The user can then select the index of the words they want to swap. Your program should swap those two words and then print the entire string to ouput. • Use string prompt "> ". • Use indices prompt "Enter two indices: ". • Remove the trailing newline character from the string. • Save your code as swap_words.c.

Example Run > You can tell a lot about a place by how they treat their people Enter two indices: 7 13 You can tell a lot about a people by how they treat their place

How to write in C?

Homework Answers

Answer #1

code:

#include<stdio.h>
#include<string.h>
int main()
{
        /*string is to strore input*/
        /*words is a 2d array to store words*/
        char string[10000];
        char words[100][100],t[100];
        int strpos,wordpos,count,i,first,second,temp;
        printf(">");
        scanf("%[^\n]s",&string);
        printf("Enter tow indices:");
        scanf("%d%d",&first,&second);
        strpos=0;
        wordpos=0;
        count=0;
        /*strpos is iterator for string */
        /*count is the number of words*/
        /*wordpos is the iterator for word*/
        while(string[strpos]!='\0')
        {
                if(string[strpos]==' ')
                {
                        wordpos=0;
                        strpos++;
                        count++;
                }
                else
                {
                        words[count][wordpos]=string[strpos];
                        strpos++;
                        wordpos++;
                }
                
        }
        temp=0;

                strcpy(t,words[first]);
                strcpy(words[first],words[second]);
                strcpy(words[second],t);
                
        
        i=0;
        /*printing the swaped line */
        while(i<=count)
        {
                printf("%s ",words[i]);
                i++;
        }
}

IF YOU HAVE ANY DOUBT OR IF YOU ENCOUNTER LOGICAL ERROR PLEASE LEAVE A COMMENT.

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
Create program that sorts words of a string based on first letter of each word. Use...
Create program that sorts words of a string based on first letter of each word. Use C programming language. - Only use stdio.h and string.h - Remove any newline \n from input string - Use insert function - Input prompt should say "Enter string of your choice: " - Output should print sorted string on new line Example:     Enter string of your choice: this is a string     a is string this
Create a function in MIPS using MARS to determine whether a user input string is a...
Create a function in MIPS using MARS to determine whether a user input string is a palindrome or not. Assume that the function is not part of the same program that is calling it. This means it would not have access to your .data segment in the function, so you need to send and receive information from the function itself. The program should be as simple as possible while still using necessary procedures. Follow the instructions below carefully. Instructions: ●...
Write a Python program to ask user input of a string, then ask user input of...
Write a Python program to ask user input of a string, then ask user input of what letters they want to remove from the string. User can either put in "odd", "even" or a number "n" that is greater than 2. When user input "odd", it will remove the characters which have odd numbers in the sequence of the string. When user input "even", it will remove the characters which have even numbers in the sequence of the string. When...
MIPS ASSEMBLY Have the user input a string and then be able to make changes to...
MIPS ASSEMBLY Have the user input a string and then be able to make changes to the characters that are in the string until they are ready to stop. We will need to read in several pieces of data from the user, including a string and multiple characters. You can set a maximum size for the user string, however, the specific size of the string can change and you can’t ask them how long the string is (see the sample...
Create a program that allows the user to input a list of first names into one...
Create a program that allows the user to input a list of first names into one array and last names into a parallel array. Input should be terminated when the user enters a sentinel character. The output should be a list of emial addresses where the address is of the following form: [email protected] Declare FirstName[100] as String Declare LastName[100] as String Declare email as String Declare K as Integer Declare index as Integer Write "Enter first and last name." Write...
Write a Java program that prompts the user to input a word (String). The program must...
Write a Java program that prompts the user to input a word (String). The program must print the reversed word with all consecutive duplicate characters removed. The program must contain the following classes: - The StackX class (you can use the Java Stack class). - The Reverse class which must contain a private data field called “word” of type string, a constructor, and a void method called revNoDup(). The revNoDup() method must reverse the word and remove the consecutive duplicate...
Write a program that accepts an input string from the user and converts it into an...
Write a program that accepts an input string from the user and converts it into an array of words using an array of pointers. Each pointer in the array should point to the location of the first letter of each word. Implement this conversion in a function str_to_word which returns an integer reflecting the number of words in the original string. To help isolate each word in the sentence, convert the spaces to NULL characters. You can assume the input...
Using Python, students will use variables, input, and printing to create a Mad Lib. The program...
Using Python, students will use variables, input, and printing to create a Mad Lib. The program will print out the title of the Mad Libs story, as well as a short explanation of game play: The program should then prompt the user to enter in nouns, verbs, adjectives, proper nouns, and adverbs: Enter a proper noun: Enter a place: Enter another place: Enter an adverb: Enter a noun: Enter an adjective: Enter an adverb: Enter a verb: Enter a place:...
15.20 Ch 9 Warm up: Parsing strings (C++) (1) Prompt the user for a string that...
15.20 Ch 9 Warm up: Parsing strings (C++) (1) Prompt the user for a string that contains two strings separated by a comma. (1 pt) Examples of strings that can be accepted: Jill, Allen Jill , Allen Jill,Allen Ex: Enter input string: Jill, Allen (2) Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input contains a comma, then assume that the input also contains...
Write a program that prompts the user to input a string and outputs the string in...
Write a program that prompts the user to input a string and outputs the string in uppercase letters. (Use dynamic arrays to store the string.) my code below: /* Your code from Chapter 8, exercise 5 is below. Rewrite the following code to using dynamic arrays. */ #include <iostream> #include <cstring> #include <cctype> using namespace std; int main() { //char str[81]; //creating memory for str array of size 80 using dynamic memory allocation char *str = new char[80]; int len;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT