I wrote this program, which is supposed to have the user put input in (maximum 40 characters), and if it contains a character which is not a letter, the program will print out "incorrect input", and if they are all letters, then the program will print out the original string, only with after every letter, it will print out it's opposite size equivalent.
An example: Input: tyhSg
Output: tTyYhHSsgG
When I try to run the program it just goes to the end, with "Press any key to continue . . .", and there is no input and no output.
I know that my program is not very well written at all. I am a beginner, so I really need somebody to explain step by step what I am doing wrong.
Thanks to all who answer.
Here is the program:
#include <iostream>
using namespace std;
void old_string(char[]);
int run_check(char[]);
int change_string(char[]);
char string[40];
int main()
{
void old_string(char[]);
void run_check();
int change_string();
I cannot really locate the exact problem, but I think it might be there. If it is, how do I fix that? (I basically tried to print out the letter and it's opposite size equivalent-just like I explained in the beginning of the program).
That is char(string-32), If you want to output the string in uppercase use the toupper() function (notice that it works with only 1 character per time)
#include <stdio.h> // load the C standard input output library
bool is_alpha(char str[]) // create a Boolean function is_alpha to take a string (str) & return if it is alpha (ie, only letters a-z / A-Z)
{
for(unsignedint c=0; str[c]!='\0'; c++) // start a loop, counter of "c" from 0 to the end of the string '\0' (end of string)
{
if(!( (str[c]>='a'&&str[c]<='z') || (str[c]>='A'&&str[c]<='Z') )) // if the character is NOT a-z OR A-Z
{
returnfalse; // then it is not alpha so return false
}
}
returntrue; // if the function reaches here - the string is alpha
}
void do_opposite(char str[]) // create a subroutine to do the weird opposite cased character thing needed...
{
for(unsignedint c=0; str[c]!='\0'; c++) // start a loop, counter of "c" from 0 to the end of the string '\0' (end of string)
{
if(str[c]>='a'&&str[c]<='z') // if the character is a-z, then print the character & then the upper-case version of it
{
printf("%c%c",str[c],str[c]-32);
}
else // otherwise (the character is A-Z), print the character & then the lower-case version of it
{
printf("%c%c",str[c],str[c]+32);
}
}
}
int main() // create the main function
{
char str[40]; // create a string of length 40
printf("Enter string: "); // tell the user to enter a string
gets(str); // get the keyboard input
if(is_alpha(str)) // if the string is alpha (only characters)
{
do_opposite(str); // then print the opposite stuff needed
}
else // otherwise
{
printf("String is not alpha (only characters)\n"); // tell the user they entered an invalid string
}
getchar(); // let them see the input, before they press enter to terminate the program
return 0; // end the main function with no errors
}