It is a big program. I stripped off unnecessary code. I left only one of the key functions
When I call ss();
in any function the function gives control back to main()
without accepting a string.
The code works if I don't use a function to accept the string. I can't find anything wrong with it.
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void ss();
void casechange();
using namespace std;
char str[100];
int main (){
int choice;
cout<<"Make a choice"<<endl;
cout<<"Press 1 to change the case of alphabets"<<endl;
cout<<"Press 2 to count number of vowels"<<endl;
cout<<"Press 3 to check if entered string is a palindrome or not"<<endl;
cout<<"Press 4 to reverse a string"<<endl;
cout<<"Press 5 to count number of words"<<endl;
cin>>choice;
switch(choice){
case 1: casechange();
break;
case 2: vowelcount();
break;
case 3:pal();
break;
case 4: rev();
break;
case 5: wordcount();
break;
default: cout<<"Wrong choice"<<endl;
}
return 0;
}
void casechange(){
ss();
for(int i=0;str[i]!='\0';i++)
{
if(isupper(str[i]))
str[i]=tolower(str[i]);
else str[i]=toupper(str[i]);
}
puts(str);
}
void ss()
{
cout<<"Enter a string"<<endl;
gets(str);
}
p.s. I am using code blocks. gcc compiler I guess.
You asked user to make a choice. User typed a number and enter
. You then read a single character. enter
still sitting there in the buffer. When it comes to gets
, it reads it as an empty string.
Also please pay attention to all the comments about IO, gets
etc.