Hi,

I have a small problem after working on my program last night i found out
that C does not have any function that uppercase first character in a word.

So i have been trying to split each word in to characters and uppercase the first
word but it keeps replacing the first character whit a whitespace

It there any other way around this ?

Recommended Answers

Without seeing your code, all I can say is that the error is on line 42.

Jump to Post

Hello n3red,
According to your code each character in the string character array will be changed to upper case. Your code will not change the first character of the string to white space.

Jump to Post

Let's start with = is not the same as == in C. toupper(lines[i=0]) is also interesting, and very likely not what you wanted.

Jump to Post

All 10 Replies

Without seeing your code, all I can say is that the error is on line 42.

commented: :) +12
for (i=0;i<theLength;i++)
    theString[i] = toupper(theString[i]);
	
return 1;

Iam trying to count the word and when it comes to the first char turn in in to upper. Iam kinda a novice this would be much easier in python and php xD

topper(*string); 

	for( ; *string != '\0'; string++)
	{
	
	if(temp == ' ')
	toupper(*string);

	
	temp = *string;
	}

Hello n3red,
According to your code each character in the string character array will be changed to upper case. Your code will not change the first character of the string to white space.

Here is my code help xD

As you can see i successfully turn "char lines" in to lowercase characters the entire text. Now in second "for" statement i am trying to lookup each word in
"char lines" count back and if it eaquals "isspace" then go +1 character and
capitalize it. But iam getting a complile error.

Iam up to my 3rd coffe, why cant there just be a simple ucfirst function xD

Basically i need to write a function that will uppercase the first character.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(){

	FILE *open;
	open = fopen("vitranc.txt","r");

	char lines[300];
	//char tmp = '\0';

	while(fgets(lines,300,open))
	{
		
		for(int i=0; lines[i]; i++){
			if(lines[i-1]==isspace){
			lines[i] = toupper(lines[i]);
			}
		}

		
	}
	printf("\n\n");

	fclose(open);

}

Let's start with = is not the same as == in C. toupper(lines[i=0]) is also interesting, and very likely not what you wanted.

Tweeked the code but.. still having problems

U can try dizz...

If you are using FILE...read only the first character of word convert it into uppercase using toupper and leave all the characters in a word as it is..if there is a white space " ",again convert the first character into upper case..till there is white space..
if EOF is reached...condition breaks..

Is there a better definition of problems? Can you explain what problems mean now? If you changed code, maybe if we know what the code looks like now it would be helpful, especially since we don't know what was tweaked nor how.

For anyone that may in future have this problem i have solved it the
for and if statements i did were correct thinking but i just did it a bit
wrong xD

Here is how you capitalize the first character of every word in a block of text
or a sentence etc:

char lines[300] or char lines = "some random text if its not feed'ed from a external file"

for(int i=0; lines[i]; i++){
    if(isspace(lines[i-1])){
	lines[i] = toupper(lines[i]);
    }
}

The first iteration of the loop invokes undefined behavior because you're accessing lines[-1]. But good on you for figuring out what was wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.