Strings!
Mar 23, 2010 at 6:05pm UTC
Hey, I'm trying to write a program which does the following:
Takes a string, displays it in all capitals
displays it in all lower case letters
Counts the amount of spaces
Counts the amount of words
I've been playing with this for awhile now, and I have a few error c2109; subscript requires array or pointer errors..I'm not sure what to do! Erros are in lines 68,75,85,94,98(twice)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
int main()
{
int i, num, chars, count=0, spaces, space, string, counter, input;
#define msize 101
#define TRUE 1
#define FALSE 0
char message[msize];
printf("Please Enter a String\n" );
gets(message);
{
for (i=0; message[i] != '\0' ; i++)
message[i] = toupper(message[i]);
}
{
printf("You entered: \n" );
puts(message);
}
{
for (i=0; message[i] != '\0' ; i++)
message[i] = tolower(message[i]);
}
{
printf("You entered: \n" );
puts(message);
}
num = 0;
{
for (i=0; message[num] != '\0' ; num++);
chars = num;
}
printf("There are %d characters\n" ,chars);
int wordcount(char string[]);
{
int numwords = 0;
int i = 0;
int letter = FALSE;
int word = TRUE;
char end = FALSE;
char done;
while (end == FALSE)
{
letter = isalpha(string[i]);
i++;
if (letter == TRUE)
{
do
{
word = isalpha(string[i]);
i++;
if (word == FALSE)
{
numwords++;
letter = FALSE;
}
}
while (letter == TRUE);
}
done = string[i];
if (done == -2)
end = TRUE;
}
return (numwords);
}
for ( counter = i; input[ counter ]!= '\0' ; counter++)
{
if (input[ counter ] == ' ' && input[ counter + 1 ] != ' ' )
spaces++;
}
_getch();
return (0);
}
here's my code so far, any help would be great! :P
Last edited on Mar 23, 2010 at 6:25pm UTC
Mar 23, 2010 at 7:16pm UTC
Made some progress, everything works now...however I'm having trouble getting the 'number of words' to actually print....silly problem but just can't seem to do it
I'm getting errors, or a very large number. I know it's probably something simple...just can't quite grasp the issue yet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
int wordcount(char []);
int main()
{
int i, num, chars, count=0, spaces=0, counter, numwords;
#define msize 101
#define TRUE 1
#define FALSE 0
char message[msize];
printf("Please Enter a String\n" );
gets(message);
{
for (i=0; message[i] != '\0' ; i++)
message[i] = toupper(message[i]);
}
{
printf("You entered: \n" );
puts(message);
}
{
for (i=0; message[i] != '\0' ; i++)
message[i] = tolower(message[i]);
}
{
puts(message);
}
num = 0;
{
for (i=0; message[num] != '\0' ; num++);
chars = num;
}
printf("There are %d characters\n" ,chars);
printf("there are %d words\n" , numwords);
for (counter = 1; message[ counter ]!= '\0' ; counter++)
{
if (message[ counter ] == ' ' && message[ counter + 1 ] != ' ' )
spaces++;
}
_getch();
return (0);
}
int wordcount(char string[])
{
int numwords = 0;
int i = 0;
int letter = FALSE;
int word = TRUE;
char end = FALSE;
char done;
while (end == FALSE)
{
letter = isalpha(string[i]);
i++;
if (letter == TRUE)
{
do
{
word = isalpha(string[i]);
i++;
if (word == FALSE)
{
numwords++;
letter = FALSE;
}
}
while (letter == TRUE);
}
done = string[i];
if (done == -2)
end = TRUE;
}
return (numwords);
}
my current print statement is on line 55, if I have it anywhere else, nothing comes up at all...yet in line 55 I seem to get errors
Last edited on Mar 23, 2010 at 7:16pm UTC
Topic archived. No new replies allowed.