-
Help with string program
Hi, I need urgent help for this program, sorry I am new to C and I got this code:
Code:
#include <stdio.h>
#define STRINGSIZE 100
void getString(char *string);
void reverseCase(char *string);
int numGraph(char *string);
int main()
{
char string[STRINGSIZE];
int count;
char c;
getString(string);
printf("Entered string => %s", c);
count = numGraph(string);
printf("Number of graphic characters in string = %d\n", count);
reverseCase(string);
printf("Reverse case => %s", c);
return 0;
}
void reverseCase(char *string)
{
/* Reverse the case of all alphabetic characters in the string.
That is, all upper case characters become lower case and
all lower case become upper case.
*/
char c;
while((c = getchar()) != EOF)
putchar(isupper(c) ? tolower(c) : toupper(c));
}
int numGraph(char *string)
{
/* Calculate the number of printable graphic characters in the
string.
*/
int count;
while (string != '\0')
{
if (isgraph((int) *string)) count++;
}
return count;
}
void getString(char *string)
{
char c;
printf("Please enter a string to process\n");
scanf("%s", c);
fgets(string, STRINGSIZE, stdin);
}
What I am trying to achieve is for the program to first ask me to input a string, then I need the program to show me the string I entered, count the number of characters entered and then convert the string to upper case or vice versa depending on the string I entered.
This code is for some reason throwing me a runtime error, I am not sure what I am doing wrong. Any help will be appreciated. Thanks.
-
Code:
char c;
scanf("%s", c);
The you've said its a string, yet 'c' is infact a char (use %c) although this statement is pointless...
isalpha, tolower ect are declared in ctypes.h (your not including this).
Theres no need to cast the arg to int in isgraph((int) *string))...
Why not count the chars with strlen() from string.h???
hth..
-
OK, thanks, I have changed the code to this:
Code:
#include <stdio.h>
#include <ctype.h>
#define STRINGSIZE 100
void getString(char *string);
void reverseCase(char *string);
int numGraph(char *string);
int main()
{
char string[STRINGSIZE];
int count;
getString(string);
printf("Entered string => %s", string);
count = numGraph(string);
printf("Number of graphic characters in string = %d\n", count);
reverseCase(string);
printf("Reverse case => %s", string);
return 0;
}
void reverseCase(char *string)
{
/* Reverse the case of all alphabetic characters in the string.
That is, all upper case characters become lower case and
all lower case become upper case.
*/
int c;
while((c = getchar()) != EOF)
putchar(isupper(c) ? tolower(c) : toupper(c));
}
int numGraph(char *string)
{
/* Calculate the number of printable graphic characters in the
string.
*/
int count;
while (string != '\0')
{
if (isgraph (*string)) count++;
}
return count;
}
void getString(char *string)
{
printf("Please enter a string to process\n");
fgets(string, STRINGSIZE, stdin);
}
It is only showing me the entered string and not executing numGraph or reverseCase, not sure why not. :(
-
It would be a good idea to at read up on the of C (pointers, loops, ect)...
Your problem lies in numGraph... You have an infinite loop...
Code:
int numGraph(char *string)
{
/* Calculate the number of printable graphic characters in the
string.
*/
int count;
while (string != '\0')
{
if (isgraph (*string)) count++;
}
return count;
}
Well 'string' is most probably never going to be NULL (you don't change its value anywhere) = infinite loop.
Redesign it...
-
Hi, I'd like to first of all thank people for the help, my little string converter program is nearly finished, I am only stuck on the function of converting the letters from the string from lower case to upper case and vice versa, this is what the code looks like now:
Code:
#include <stdio.h>
#include <ctype.h>
#define STRINGSIZE 100
void getString(char *string);
void reverseCase(char *string);
int numGraph(char *string);
int main()
{
char string[STRINGSIZE];
int count;
getString(string);
printf("Entered string => %s\n",string);
count = numGraph(string);
printf("Number of graphic characters in string = %d\n", count);
reverseCase(string);
printf("Reverse case => %s\n",string);
return 0;
}
void reverseCase(char *string)
{
/* Reverse the case of all alphabetic characters in the string.
That is, all upper case characters become lower case and
all lower case become upper case.
*/
while((*string = getchar()) != EOF)
putchar(isupper(string) ? tolower(string) : toupper(string));
}
int numGraph(char *string)
{
/* Calculate the number of printable graphic characters in the
string.
*/
int i,count;
for(i = 0,count = 0;string[i] != '\0';i++)
{
if (isgraph((int)string[i])) count++;
}
return count;
}
void getString(char *string)
{
printf("Please enter a string to process\n");
fgets(string, STRINGSIZE, stdin);
}
It compiles and runs fine until it gets to the reverseCase function that thrwos me a segmentation fault. Anyone know what I am doing wrong?
Thanks
-
Code:
void reverseCase(char *string)
{
/* Reverse the case of all alphabetic characters in the string.
That is, all upper case characters become lower case and
all lower case become upper case.
*/
while((*string = getchar()) != EOF)
putchar(isupper(string) ? tolower(string) : toupper(string));
}
This code reads from the console instead of reading from the string. You need to change this into a loop just like in numGraph and then change the value inside the array string instead of writing to the console.
-
Hi pplz, I managed to get the program working correctly now using this code:
Code:
#include <stdio.h>
#include <ctype.h>
#define STRINGSIZE 100
void getString(char *string);
void reverseCase(char *string);
int numGraph(char *string);
int main()
{
char string[STRINGSIZE];
int count;
getString(string);
printf("Entered string => %s",string);
count = numGraph(string);
printf("Number of graphic characters in string = %d\n", count);
reverseCase(string);
printf("Reverse case => %s", string);
return 0;
}
void reverseCase(char *string)
{
/* Reverse the case of all alphabetic characters in the string.
That is, all upper case characters become lower case and
all lower case become upper case.
*/
int i;
for (i = 0; string[i] != '\0'; i++)
{
if (isgraph((int)string[i]))
{
if (islower(string[i])) string[i] = toupper(string[i]);
else if (isupper(string[i])) string[i] = tolower(string[i]);
}
}
}
int numGraph(char *string)
{
/* Calculate the number of printable graphic characters in the
string.
*/
int i, count = 0;
for (i = 0; string[i] != '\0';i++)
{
if (isgraph((int)string[i])) count++;
}
return count;
}
void getString(char *string)
{
printf("Please enter a string to process\n");
fgets(string, STRINGSIZE, stdin);
}
Now, this program compiles correctly with no errors and executes correctly too and produces the desired output, so you might be asking what the problem is. Well, I have to submit this file via a system that uses a special compiler and this annoying compiler throws me the following error in these two lines:
if (islower(string[i])) string[i] = toupper(string[i]);
else if (isupper(string[i])) string[i] = tolower(string[i]);
Error: Warning: subscript has type 'char'
And therefore this compiler won't let me submit this file, I am not sure why it is giving me such error, it compiles and runs fine with my C compiler but this other compiler that I need it to compile fine too throws me these errors. Can anyone suggest why? And how can I fix this? Any help would be enormously appreciated.
-
islower and isupper takes as parameter a character, casted to an int. See what you did for isgraph and do the same for islower/isupper.
-
Never mind guys, I fixed it up now, I had to cast the string[i] to int on those two lines as well. Fixed and submitted now, thanks for your help :)