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
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
NOTE ABOUT QUESTION: -
- The above code makes use of c++ functions like getline() to read the entire line as a string.
- In every function, a string is passed as an argument to function, and the function returns the string.
NOTE ABOUT SOLUTION CODE: -
- The string is not a data type in c++, the string is represented as char[] in c language.
- 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: -
- 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.
- 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.
- 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.
//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
This is a popular solution!
Step by step
Solved in 3 steps with 2 images