Convert small letter to capital letter - c

I'm trying to convert string in small letter to capital letter.
I got some error (access volation)
what whould do?
int main()
{
char str[10];
int i=0;
scanf("%s", &str);
while (str[i] !=0)
{
str[i] += -32;
printf("%s", str[i]);
}
return 0;
}
thx

If you enter a string longer than 9 characters, scanf() will try to write past the end of your string buffer.
Your while-loop never terminates as you never change i.
You should use "%c" as format string in your printf() call, since you are wrting characters, not null-terminated strings.

int main()
{
char str[10];
int i=0;
scanf("%s", str);
while (str[i] != 0)
{
str[i] += -32;
i++;
}
printf("%s", str);
return 0;
}
and of course, you must check overflow of str...

As cprogrammer said
You better use toupper
/* toupper example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
int i=0;
char str[]="Test String.\n";
char c;
while (str[i])
{
c=str[i];
putchar (toupper(c));
i++;
}
return 0;
}
But if not, and you want to do it your way
int main()
{
char str[10];
int i=0;
scanf("%s", &str);
while (str[i]!='\0' && i<10)
{// You forgot this: '\0' instead of 0 and also i<10
str[i] += -32;
printf("%c", str[i]);//char, not string
i++; //And this
}
return 0;
}

There are few mistakes here:
scanf("%s", &str); - since str is a pointer to char, you don't need to give its address, but scanf("%s", str);. (and as sven said, it's unsafe)
while (str[i] !=0) this is an endless loop, you should increment i at the end of the while block.
str[i] += -32; will modify any char you're at, you should check if this is a lower case any time, for example:
if (str[i] >= 'a' && str[i] <= 'z'){
str[i] -=32;
} //couldn't format this line for some reason....
printf("%s", str[i]) is again wrong way to use printf, since %s expects to char*, and str[i] is a char. instead, use printf("%c", str[i]) which expects a char

You better use toupper
/* toupper example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
int i=0;
char str[]="Test String.\n";
char c;
while (str[i])
{
c=str[i];
putchar (toupper(c));
i++;
}
return 0;
}

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main();
{
int i, count;
char str[200];
clrscr();
printf("Enter a string");
scanf("%s", str);
count = strlen(str);
for(i=0; i<=count; i++)
{
if((str[i] >= 97) && (str[i] <= 122))
{
str[i] = str[i] - 32;
}
}
printf("%s", str);
getch();
}

You can use Bitwise AND ( & ) operator technique to make small letter to capital letter.
char ch = letter & 223; [letter = a-z] // Now ch is all time capital letter

Related

How to have a function output a string, not just the first word

I'm writing a function to capitalize every lowercase character in a string. It takes a string from the user and that is the input to the function. My program works if the user doesn't enter spaces, but when there is a space in the string, the function ends.
#include <stdio.h>
char *uppercase(char *c) {
int i = 0;
while (c[i] != '\0') {
if (123 > c[i] && c[i] > 96)
c[i] = c[i] - 'a' + 'A';
i++;
}
return c;
}
int main() {
char input[100];
printf("Enter the phrase: ");
scanf("%s", input);
printf("%s", uppercase(input));
return 0;
}
Examples:
Input: test test
Output: TEST
Input: Hello
Output: HELLO
Input: How are you
Output: HOW
I think it has to do with the while statement? Thanks for the help.
The problem is not in the while statement, but rather due to the scanf() format: %s reads a single word from the input, leaving the rest of the line in the stdin buffer. Note also that typing a word with more than 99 characters will cause undefined behavior because scanf() will write beyond the end of the input array. Using %99s would prevent this problem, but not solve your issue.
You should use fgets() to read a full line of input from stdin.
Furthermore, instead of using hard coded ASCII values, you should use character constants such as 'a' and 'z' or the functions from <ctype.h>.
Here is a modified version using ASCII:
#include <stdio.h>
char *uppercase(char *s) {
int i = 0;
while (s[i] != '\0') {
if (s[i] >= 'a' && s[i] <= 'z')
s[i] = s[i] - 'a' + 'A';
i++;
}
return s;
}
int main() {
char input[100];
printf("Enter the phrase: ");
if (fgets(input, sizeof input, stdin)) {
fputs(uppercase(input), stdout);
}
return 0;
}
Here is a more portable one using <ctype.h>:
#include <ctype.h>
#include <stdio.h>
char *uppercase(char *s) {
int i = 0;
unsigned char c;
while ((c = s[i]) != '\0') {
if (islower(c))
s[i] = toupper(c);
i++;
}
return s;
}
This can be further simplified as toupper() can be called for all byte values from 0 to UCHAR_MAX:
#include <ctype.h>
#include <stdio.h>
char *uppercase(char *s) {
for (size_t i = 0; s[i] != '\0'; i++) {
s[i] = toupper((unsigned char)c);
}
return s;
}
Either use
scanf ("%99[^\n]", input);
or
fgets( input, sizeof( input ), stdin );
Also it is a bad practice to use magic numbers like 123 or 96
if (123 > c[i] && c[i] > 96)
The code will be more readable if you will write at least
if ( 'a' <= c[i] && c[i] <= 'z' )
Also instead of the while loop it is better to use the for loop like
for ( char *p = c; *p; ++p )
{
if ( 'a' <= *p && *p <= 'z' )
{
*p = *p - 'a' + 'A';
}
}

How can I write a function whose prototype should be 'void convertstring(char *)' to convert lower case to upper case in C?

This is what I've written
#include <stdio.h>
void convertstring(char *str[])
{
int i;
for (i = 0; *str[i] != '\0'; i++)
{
if (*str[i] >= 'a' && *str[i] <= 'z')
{
*str[i] = *str[i] - 32;
}
}
}
int main()
{
char str[100];
gets(str);
convertstring(&str);
printf("Uppercase string : %s", str);
return 0;
}
There is no output at all in this case. I want to return the the full text in upper case characters while using the void function. This could be done using other types of functions but this is the challenge that the function must be of type void convertstring(char *)
There are several issues with your code:
First of all, you're using gets, which was deprecated in C99 and removed in C11. Instead, use fgets:
fgets(str, sizeof(str), 100);
Second, you're passing a pointer to an array to the function. You shouldn't do that.
Along with the last point, you're operating on a char *[] in your function. Instead, use a char *, like so:
void convertstring(char *str)
{
int i;
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
str[i] = str[i] - 32;
}
}
}
You really ought to get a good book on C or learn about pointers.

making a function of turning lowercase string into uppercase string in c

I'm still a beginner at making function and C programming.
I'm trying to make a function for turning it into uppercase, but it seems I messed it up at the pointer(?)
#include <stdio.h>
void mytoupper(char *s[]) {
int i = 0;
while (s[i] != '\0') {
if (s[i] >= 'a' && s[i] <= 'z') {
s[i] = s[i] - 32;
}
i++;
}
return s;
}
int main(void) {
char s[32];
printf("Insert string:");
printf("%s", s);
printf("%s", mytoupper(s[32]));
return 0;
}
There are multiple problems in your code:
the definition for mytoupper is incorrect: it should take a char *s argument instead of char *s[], and return char *.
Changing the character from lower to upper case should not use a hard coded value of 32 that only works for ASCII, use a more generic approach with s[i] = s[i] - 'a' + 'A';
To read the string, use scanf("%31s", s); instead of printf("%s", s); and it is highly recommended to test the return value of scanf()
The argument in printf("%s", mytoupper(s[32])); is incorrect: you should just write printf("%s", mytoupper(s));
Here is a corrected version:
#include <stdio.h>
char *mytoupper(char *s) {
int i = 0;
while (s[i] != '\0') {
if (s[i] >= 'a' && s[i] <= 'z') {
s[i] = s[i] - 'a' + 'A';
}
i++;
}
return s;
}
int main(void) {
char s[32];
printf("Insert string:");
if (scanf("%31s", s) == 1) {
printf("%s\n", mytoupper(s));
}
return 0;
}
There are a few spots that will not work as expected in your code.
You are not properly getting user input, instead you are trying to print an 'empty' string.
printf("%s",s);
You could change this to:
scanf("%31s",s);
You are accepting an array of char pointers in your function mytoupper, but this is not needed. You can instead simply pass it an array of chars.
void mytoupper(char s[])
You are attempting to use a return value of a void function. You can either call the function then print the string, or have the function return the string.
If you want to change the string then print it seperately you will need to change mytoupper to no longer return anything and keep it as a void return type.
mytoupper(s);
printf("%s", s);
Or change the function to:
char* mytoupper(char s[]) {
Then print the string:
printf("%s", mytoupper(s));

Identical C Code Behaves Differently

I declared an empty string:
char str[MAX_LEN] = "\0"; //empty String
and then
void InitString(char *str,int maxlenght)
{
char input = 0;
int counter = 0,i;
for(i = 0;i<(maxlenght);i++)
{
*(str+i) = '\0';
}
getchar();
printf("\nEnter new string of max %d chars: ",maxlenght);
while (input != '\r' && counter < (maxlenght-1))
{
input = getche();
*(str+counter) = input;
counter++;
}
}
void PrintString(char *str)
{
int i = 0;
printf("\nThe String Created is : ");
puts(str);
while(*(str+i) != '\0')
{
printf("%c", *(str+i));
i++;
}
}
I have no idea why this code behaves differently since the code is identical in logic to the upper one.
int CountWords(char *str)
{
int i = 0;
char ch;
while(*(str+i) != '\0')
{
printf("%d", *(str+i));
ch = *(str+i);
printf("%c",ch);
numNumber++;
i++;
}
return i;
}
There is no output for the lower code block even though the test condition is the same.
Your problem is that you are using puts(). This states
The C library function int puts(const char *str) writes a string to stdout up to but not including the null character. A newline character is appended to the output.
Thus, if puts() uses strlen() then your '\0' is replaced by a '\n' and that is why your while loop doesn't work.

How to store array in address C

basically we are provided with the main function:
#include <stdio.h>
#include <string.h>
#include <strings.h>
#define STORAGE 255
{
int c;
char s[STORAGE];
for(;;) {
(void) printf("n=%d, s=[%s]\n", c = getword(s), s);
if (c == -1)break;
}}
we are not to change that
and we have to create the function getword() which should contain a loop that reads a characters,
store it in a array in the address provided one by one and it should stop for whitespace (tab, space, eof)
basically i have this:
int getword(char *w)
{
char str[255];
int i = 0;
int charCount = 0;
printf("enter your sentence:\n");
gets(str);
/*this was provided by the professor, but i'm not sure how to use it
while (( iochar - getchar()) !=EOF);
*/
for(i = 0; str[i] != '\0'; i++) //loop that checks tab and spaces
{
if(str[i] != ' ' && str[i] != '\t')
{
charCount++;
}
}
printf("your string: '%s' contains %d of letters\n", str, charCount);
return 0;}
right now the output of the program is:
enter your sentence:
hey stockoverflow
your string: 'hey stockoverflow' contains 16 of letters
n=0, s=[]
so i'm saving the string and counting it, but i'm not storing it where i should be.
it should display n=16, s=[hey stockoverflow]
actually it should display n=3, s=[hey]
any help would be appreciated
This may be what you are looking for:
for(i = 0; str[i] != '\0'; i++) //loop that checks tab and spaces
{
if(str[i] != ' ' && str[i] != '\t')
{
charCount++;
}
else
{
str[i] = '\0'; // Terminate str
break; // Break out of the for-loop
}
}
printf("your string: '%s' contains %d of letters\n", str, charCount);
strcpy(w, str); // Add this line to copy str into w (which is s from main)
return strlen(w); // Return the length of the result string

Resources