CONVERT AND CHECK my codes   USE : C PROGRAMMING thank you !!! #include #include using namespace std; string swapLetters(string input) { for (int i = 1; i < input.length() / 3; i++) { char ch = input[i]; input[i] = input[input.length() - 1 - i]; input[input.length() - 1 - i] = ch; } return input; } string userPrompt(string input) { printf( "Enter a WORD: "; getline(cin, input, '.'); return input; } string toLower(string string) { for (int i = 0; i < string.length(); i++) { string.at(i) = tolower(string.at(i)); } return string; } int main() { string userInput; userInput = userPrompt(userInput); userInput = toLower(userInput); printf("You input: " ); userInput = swapLetters(userInput); printf(  "Scrambled: "); return 0; }   sample output should be :    word : handsome  scramble word : Hsomeand word: teacher scramble word : thereach the first letter stay same in scramble

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 6PE
icon
Related questions
Question

please CONVERT AND CHECK my codes 

 USE : C PROGRAMMING

thank you !!!

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

using namespace std;

string swapLetters(string input)
{
for (int i = 1; i < input.length() / 3; i++)
{
char ch = input[i];
input[i] = input[input.length() - 1 - i];
input[input.length() - 1 - i] = ch;
}
return input;
}
string userPrompt(string input)
{
printf( "Enter a WORD: ";
getline(cin, input, '.');
return input;
}
string toLower(string string)
{
for (int i = 0; i < string.length(); i++)
{
string.at(i) = tolower(string.at(i));
}
return string;
}

int main()
{
string userInput;
userInput = userPrompt(userInput);
userInput = toLower(userInput);

printf("You input: " );

userInput = swapLetters(userInput);
printf(  "Scrambled: ");
return 0;
}

 

sample output should be : 

 

word : handsome 

scramble word : Hsomeand

word: teacher

scramble word : thereach

the first letter stay same in scramble

Expert Solution
Step 1

NOTE ABOUT QUESTION: -

  1. The above code makes use of c++ functions like getline() to read the entire line as a string.
  2. In every function, a string is passed as an argument to function, and the function returns the string.

NOTE ABOUT SOLUTION CODE: -

  1. The string is not a data type in c++, the string is represented as char[] in c language.
  2. There is no need of returning the string from function, in all functions the modification is being done in original char[] passed from the main function.

FUNCTIONS: -

  1. The user input is read character by character using getchar() function in a do..while loop. In every iteration, it is being checked if the user enters '\n' character or if string input length exceeds size of char[] which is 84 fixed. userPrompt() function takes an input string and stores in char[] passed from the main function, thus actual input is stored in char[]. So, there is no need to return the char[] from function unlike as done in question code.
  2. The function toLower() converts each character to lowercase by traversing the char[] in a while..loop. Function tolower() is present in the string.h header file.
  3. Scramble the string using swapLetters() function. Here, each character is swapped and stored in actual char[] passed from main function, so No need to return char[] from function.

In the end, the scrambled string is shown on the console from the main function.

 

Step 2

//WORKING CODE

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

//tolower function is present in ctype header file
#include <ctype.h> 

//max chars that can be stored in char[]
#define MAX_CHARS 84

//shuffling the string
void swapLetters(char input[])
{
 int len = strlen(input);
 
 //temp char to store characters from char[]
 char tmp = ' ';
 
 int i =0;
    for (i = 1; i < len / 3; i++) {
        //assign current char to temp
        tmp = input[i];
        
        //assign char from len-i-1 to current index index i
        input[i] = input[len -i];
        
        //assign temp to len-i-1 to  index i
        input[len -i] = tmp;
    }

}
//get string as user input 
void userPrompt(char input[])
{
 //temp char to hold user input char
 char temp = ' ';
 int i =0 ;
 printf( "Enter a WORD: ");
 
 //take input till user enters new line character
 do{
  //read one character at a time
  temp = getchar();
  
  //if char is not new line
  if(temp != '\n'){
   input[i] = temp;
  }
  //increment i to next index
  i++;
 }while((i<MAX_CHARS-1) && temp != '\n');
 
 //end char array with '\0'
 input[i] = '\0';
 
}

//convert char[] to lowercase
void toLower(char string[])
{
 int i = 0;
 //traverse loop till end of string reached
 while(string[i] != '\0')
 {
  //convert current char to lowercase
  string[i] = tolower(string[i++]);
 }
}

int main()
{
 //declaring a char array of MAX_CHARS size
 char userInput[MAX_CHARS];
 
 //take input and store in userinput char[]
 userPrompt(userInput);
 
 //convert userinput to lowercase
 toLower(userInput);
 
 //swap letters to shuffle the string
 swapLetters(userInput);
 
 //scrambled string output
 printf( "\bScrambled: %s",userInput);
 
 return 0;
}

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Header Files
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT