I need to lowercase an entire string
Page 1 of 114 Replies - 216 Views - Last Post: 24 October 2011 - 07:16 PM
#1
I need to lowercase an entire string
Posted 24 October 2011 - 02:50 PM
Replies To: I need to lowercase an entire string
#2
Re: I need to lowercase an entire string
Posted 24 October 2011 - 03:04 PM
tolower reference
#3
Re: I need to lowercase an entire string
Posted 24 October 2011 - 03:04 PM
#4
Re: I need to lowercase an entire string
Posted 24 October 2011 - 03:24 PM
This is the code when I try to implement tolower.
#include <stdio.h>
#include <string.h>
/* #include <conio.h> */
char *mygetline(char *line, int size)
{
if ( fgets(line, size, stdin) )
{
/* check for trailing '\n' */
char *newline = strchr(line, '\n');
if ( newline )
{
/* overwrite the '\n' with
a terminating null
*/
*newline = '\0';
}
}
return line;
}
int main(void)
{
char text[1000] = "";
char * pch;
char * pbody;
char words[100][100];
char body[100][100];
int numWords = 0;
int numBody = 0;
int i;
/* pch = strtok (text," "); */
fputs("Input Forbidden words: ", stdout);
fflush(stdout);
printf("forbidden words = \"%s\"\n", mygetline(text, sizeof text));
/* Convert to words */
for ( pch = strtok (text, " ") ;
numWords < 100 && pch != NULL ;
pch = strtok (NULL, " "), numWords++ )
{
strcpy( words[numWords], pch );
}
/* print array of strings */
for(i=0; i<25; printf("%s ", words[i]), i++);
printf("\n");
fputs("Input body of text words: ", stdout);
fflush(stdout);
printf("Body of text = \"%s\"\n", mygetline(text, sizeof text));
for ( pbody = strtok (text, " ") ;
numWords < 100 && pbody != NULL ;
pbody = strtok (NULL, " "), numBody++ )
{
strcpy( body[numBody], pbody );
}
/* print array of strings */
for(i=0; i<25; printf("%s\n", body[i]), i++);
/*for(i=0; i<sizeof(words); i++){
if(strcmp(body[i], "") == 0){
printf("true\n");}
else{printf("false\n");}}*/
/*trying to see if any words match*/
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[0]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[0], "\n")>0){
char* q = body[0];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[1]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[1], "\n")>0){
char* q = body[1];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[2]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[2], "\n")>0){
char* q = body[2];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[3]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[3], "\n")>0){
char* q = body[3];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[4]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[4], "\n")>0){
char* q = body[4];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[5]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[5], "\n")>0){
char* q = body[5];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[6]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[6], "\n")>0){
char* q = body[6];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[7]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[7], "\n")>0){
char* q = body[7];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[8]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[8], "\n")>0){
char* q = body[8];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[9]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[9], "\n")>0){
char* q = body[9];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[10]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[10], "\n")>0){
char* q = body[10];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[11]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[11], "\n")>0){
char* q = body[11];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[12]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[12], "\n")>0){
char* q = body[12];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[13]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[13], "\n")>0){
char* q = body[13];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[14]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[14], "\n")>0){
char* q = body[14];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[15]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[15], "\n")>0){
char* q = body[15];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[16]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[16], "\n")>0){
char* q = body[16];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[17]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[17], "\n")>0){
char* q = body[17];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[18]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[18], "\n")>0){
char* q = body[18];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(tolower(words[i]), tolower(body[19]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[19], "\n")>0){
char* q = body[19];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){printf("%s ", body[i]);}
getch();
return (0);
}
this is the code that works, but doesn't do waht I want.
#include <stdio.h>
#include <string.h>
/* #include <conio.h> */
char *mygetline(char *line, int size)
{
if ( fgets(line, size, stdin) )
{
/* check for trailing '\n' */
char *newline = strchr(line, '\n');
if ( newline )
{
/* overwrite the '\n' with
a terminating null
*/
*newline = '\0';
}
}
return line;
}
int main(void)
{
char text[1000] = "";
char * pch;
char * pbody;
char words[100][100];
char body[100][100];
int numWords = 0;
int numBody = 0;
int i;
/* pch = strtok (text," "); */
fputs("Input Forbidden words: ", stdout);
fflush(stdout);
printf("forbidden words = \"%s\"\n", mygetline(text, sizeof text));
/* Convert to words */
for ( pch = strtok (text, " ") ;
numWords < 100 && pch != NULL ;
pch = strtok (NULL, " "), numWords++ )
{
strcpy( words[numWords], pch );
}
/* print array of strings */
for(i=0; i<25; printf("%s ", words[i]), i++);
printf("\n");
fputs("Input body of text words: ", stdout);
fflush(stdout);
printf("Body of text = \"%s\"\n", mygetline(text, sizeof text));
for ( pbody = strtok (text, " ") ;
numWords < 100 && pbody != NULL ;
pbody = strtok (NULL, " "), numBody++ )
{
strcpy( body[numBody], pbody );
}
/* print array of strings */
for(i=0; i<25; printf("%s\n", body[i]), i++);
/*for(i=0; i<sizeof(words); i++){
if(strcmp(body[i], "") == 0){
printf("true\n");}
else{printf("false\n");}}*/
/*trying to see if any words match*/
for(i=0; i<25; i++){
if(strcmp(words[i], body[0])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[0], "\n")>0){
char* q = body[0];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[1])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[1], "\n")>0){
char* q = body[1];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[2])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[2], "\n")>0){
char* q = body[2];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[3])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[3], "\n")>0){
char* q = body[3];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[4])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[4], "\n")>0){
char* q = body[4];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[5])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[5], "\n")>0){
char* q = body[5];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[6])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[6], "\n")>0){
char* q = body[6];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[7])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[7], "\n")>0){
char* q = body[7];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[8])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[8], "\n")>0){
char* q = body[8];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[9])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[9], "\n")>0){
char* q = body[9];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[10])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[10], "\n")>0){
char* q = body[10];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[11])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[11], "\n")>0){
char* q = body[11];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[12])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[12], "\n")>0){
char* q = body[12];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[13])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[13], "\n")>0){
char* q = body[13];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[14])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[14], "\n")>0){
char* q = body[14];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[15])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[15], "\n")>0){
char* q = body[15];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[16])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[16], "\n")>0){
char* q = body[16];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[17])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[17], "\n")>0){
char* q = body[17];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[18])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[18], "\n")>0){
char* q = body[18];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(words[i], body[19])==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[19], "\n")>0){
char* q = body[19];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){printf("%s ", body[i]);}
getch();
return (0);
}
#5
Re: I need to lowercase an entire string
Posted 24 October 2011 - 03:42 PM
#6
Re: I need to lowercase an entire string
Posted 24 October 2011 - 03:46 PM
Jim
#7
Re: I need to lowercase an entire string
Posted 24 October 2011 - 03:59 PM
#8
Re: I need to lowercase an entire string
Posted 24 October 2011 - 04:13 PM
char lowercase(char* word){
int i;
for(i=0; i<25; i++){word[i]=tolower(word[i]);}
return word;
}
and it seems to work by itself, but for some reason when i try to implement it into my code, it does not work. As soon as it gets to the matchmaking part, it crashes.
I get this warning: [Warning] return makes integer from pointer without a cast
Here is my new code:
#include <stdio.h>
#include <string.h>
/* #include <conio.h> */
char lowercase(char* word){
int i;
for(i=0; i<25; i++){word[i]=tolower(word[i]);}
return word;
}
char *mygetline(char *line, int size)
{
if ( fgets(line, size, stdin) )
{
/* check for trailing '\n' */
char *newline = strchr(line, '\n');
if ( newline )
{
/* overwrite the '\n' with
a terminating null
*/
*newline = '\0';
}
}
return line;
}
int main(void)
{
char text[1000] = "";
char * pch;
char * pbody;
char words[100][100];
char body[100][100];
int numWords = 0;
int numBody = 0;
int i;
/* pch = strtok (text," "); */
fputs("Input Forbidden words: ", stdout);
fflush(stdout);
printf("forbidden words = \"%s\"\n", mygetline(text, sizeof text));
/* Convert to words */
for ( pch = strtok (text, " ") ;
numWords < 100 && pch != NULL ;
pch = strtok (NULL, " "), numWords++ )
{
strcpy( words[numWords], pch );
}
/* print array of strings */
for(i=0; i<25; printf("%s ", words[i]), i++);
printf("\n");
fputs("Input body of text words: ", stdout);
fflush(stdout);
printf("Body of text = \"%s\"\n", mygetline(text, sizeof text));
for ( pbody = strtok (text, " ") ;
numWords < 100 && pbody != NULL ;
pbody = strtok (NULL, " "), numBody++ )
{
strcpy( body[numBody], pbody );
}
/* print array of strings */
for(i=0; i<25; printf("%s\n", body[i]), i++);
/*for(i=0; i<sizeof(words); i++){
if(strcmp(body[i], "") == 0){
printf("true\n");}
else{printf("false\n");}}*/
char c;
/*trying to see if any words match*/
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[0]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[0], "\n")>0){
char* q = body[0];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[1]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[1], "\n")>0){
char* q = body[1];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[2]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[2], "\n")>0){
char* q = body[2];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[3]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[3], "\n")>0){
char* q = body[3];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[4]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[4], "\n")>0){
char* q = body[4];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[5]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[5], "\n")>0){
char* q = body[5];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[6]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[6], "\n")>0){
char* q = body[6];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[7]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[7], "\n")>0){
char* q = body[7];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[8]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[8], "\n")>0){
char* q = body[8];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[9]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[9], "\n")>0){
char* q = body[9];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[10]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[10], "\n")>0){
char* q = body[10];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[11]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[11], "\n")>0){
char* q = body[11];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[12]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[12], "\n")>0){
char* q = body[12];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[13]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[13], "\n")>0){
char* q = body[13];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[14]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[14], "\n")>0){
char* q = body[14];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[15]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[15], "\n")>0){
char* q = body[15];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[16]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[16], "\n")>0){
char* q = body[16];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[17]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[17], "\n")>0){
char* q = body[17];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[18]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[18], "\n")>0){
char* q = body[18];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[19]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[19], "\n")>0){
char* q = body[19];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){printf("%s ", body[i]);}
getch();
return (0);
}
This post has been edited by bstfrmdamidaeast: 24 October 2011 - 04:19 PM
#9
Re: I need to lowercase an entire string
Posted 24 October 2011 - 04:15 PM
Jim
#10
Re: I need to lowercase an entire string
Posted 24 October 2011 - 04:35 PM
#11
Re: I need to lowercase an entire string
Posted 24 October 2011 - 04:43 PM
Jim
#12
Re: I need to lowercase an entire string
Posted 24 October 2011 - 05:05 PM
char lowercase(char *word){
int i;
for(i=0; i<25; i++){word[i]=tolower(word[i]);}
return *word;
}
I get this warning:[Warning] passing arg 1 of `strcmp' makes pointer from integer without a cast ."
When i try to run my program, it crashes when it reaches this part of the code, the part where i lowercase everything.
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[0]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[0], "\n")>0){
char* q = body[0];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
Here is my code:
#include <stdio.h>
#include <string.h>
/* #include <conio.h> */
char lowercase(char *word){
int i;
for(i=0; i<25; i++){word[i]=tolower(word[i]);}
return *word;
}
char *mygetline(char *line, int size)
{
if ( fgets(line, size, stdin) )
{
/* check for trailing '\n' */
char *newline = strchr(line, '\n');
if ( newline )
{
/* overwrite the '\n' with
a terminating null
*/
*newline = '\0';
}
}
return line;
}
int main(void)
{
char text[1000] = "";
char * pch;
char * pbody;
char words[100][100];
char body[100][100];
int numWords = 0;
int numBody = 0;
int i;
/* pch = strtok (text," "); */
fputs("Input Forbidden words: ", stdout);
fflush(stdout);
printf("forbidden words = \"%s\"\n", mygetline(text, sizeof text));
/* Convert to words */
for ( pch = strtok (text, " ") ;
numWords < 100 && pch != NULL ;
pch = strtok (NULL, " "), numWords++ )
{
strcpy( words[numWords], pch );
}
/* print array of strings */
for(i=0; i<25; printf("%s ", words[i]), i++);
printf("\n");
fputs("Input body of text words: ", stdout);
fflush(stdout);
printf("Body of text = \"%s\"\n", mygetline(text, sizeof text));
for ( pbody = strtok (text, " ") ;
numWords < 100 && pbody != NULL ;
pbody = strtok (NULL, " "), numBody++ )
{
strcpy( body[numBody], pbody );
}
/* print array of strings */
for(i=0; i<25; printf("%s\n", body[i]), i++);
/*for(i=0; i<sizeof(words); i++){
if(strcmp(body[i], "") == 0){
printf("true\n");}
else{printf("false\n");}}*/
char c;
/*trying to see if any words match*/
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[0]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[0], "\n")>0){
char* q = body[0];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[1]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[1], "\n")>0){
char* q = body[1];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[2]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[2], "\n")>0){
char* q = body[2];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[3]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[3], "\n")>0){
char* q = body[3];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[4]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[4], "\n")>0){
char* q = body[4];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[5]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[5], "\n")>0){
char* q = body[5];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[6]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[6], "\n")>0){
char* q = body[6];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[7]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[7], "\n")>0){
char* q = body[7];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[8]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[8], "\n")>0){
char* q = body[8];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[9]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[9], "\n")>0){
char* q = body[9];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[10]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[10], "\n")>0){
char* q = body[10];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[11]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[11], "\n")>0){
char* q = body[11];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[12]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[12], "\n")>0){
char* q = body[12];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[13]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[13], "\n")>0){
char* q = body[13];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[14]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[14], "\n")>0){
char* q = body[14];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[15]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[15], "\n")>0){
char* q = body[15];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[16]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[16], "\n")>0){
char* q = body[16];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[17]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[17], "\n")>0){
char* q = body[17];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[18]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[18], "\n")>0){
char* q = body[18];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){
if(strcmp(lowercase(words[i]), lowercase(body[19]))==0 && strcmp(words[i], "\n") > 0){
if(strcmp(body[19], "\n")>0){
char* q = body[19];
while( *q != 0 ) { *q = 'X'; ++q; }
}
}
}
for(i=0; i<25; i++){printf("%s ", body[i]);}
getch();
return (0);
}
This post has been edited by bstfrmdamidaeast: 24 October 2011 - 05:06 PM
#13
Re: I need to lowercase an entire string
Posted 24 October 2011 - 05:30 PM
Quote
main.c|8|error: implicit declaration of function ‘tolower’|
main.c||In function ‘main’:|
main.c|85|error: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|85|error: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|99|error: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|99|error: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|114|error: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|114|error: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|128|error: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|128|error: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|142|error: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|142|error: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|157|error: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|157|error: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|172|error: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|172|error: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|186|error: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|186|error: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|201|error: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|201|error: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|215|error: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|215|error: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|229|error: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|229|error: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|244|error: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|244|error: passing argument 2 of ‘strcmp’ makes pointer from integer without a cast|
/usr/include/string.h|143|note: expected ‘const char *’ but argument is of type ‘char’|
main.c|258|error: passing argument 1 of ‘strcmp’ makes pointer from integer without a cast|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build finished: 50 errors, 2 warnings ===|
For the first message, you must include ctype.h to use tolower.
The next error is on line 85:
Quote
if(strcmp(lowercase(words[i]), lowercase(body[0]))==0 && strcmp(words[i], "\n") > 0){
And then lets look at the the lowercase function:
char lowercase(char *word){
int i;
for(i=0; i<25; i++){word[i]=tolower(word[i]);}
return *word;
}
What is the return type for this function? Is it a pointer? Hint no, it's a single character. Your function declaration should be:
char* lowercase(char *word)
And you should be returning a pointer:
return word;
After I made those changes and added the ctype.h include file the code compiles with only 1 warning:
Quote
In future when you get errors when compiling your code, post the complete error messages exactly as they appear in your development environment. These messages have important information embedded in them to facilitate locating and diagnosing the errors. One of the important pieces of information is the line number where the error is being detected.
Jim
This post has been edited by jimblumberg: 24 October 2011 - 05:31 PM
#14
Re: I need to lowercase an entire string
Posted 24 October 2011 - 06:18 PM
#15
Re: I need to lowercase an entire string
Posted 24 October 2011 - 07:16 PM
char* lowercase(char *word){
int i = 0;
while(word[i] != '\0')
{
word[i]=tolower(word[i]);
++i;
}
return word;
}
This way this function should work of any sized "word".
Jim
This post has been edited by jimblumberg: 24 October 2011 - 07:17 PM
|
|

New Topic/Question
Reply



MultiQuote




|