making a function of turning lowercase string into uppercase string in c - 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));

Related

What's the error since s is string not character?

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
string replace(string s);
int main(void/*int argc, string argv[]*/)
//int main(string)
{
printf("%s\n", replace);
//return 0;
}
string replace(string s)
{
//string s;
int n;
s = get_string("Please type the word: ");
//s = tolower(s[i]);
n = strlen(s);
for (int i = 0; i < n; i++)
{
switch(tolower(s[i]))
{
case 'a' :
s[i] = '6';
continue;
case 'e' :
s[i] = '3';
continue;
case 'i' :
s[i] = '1';
continue;
case 'o' :
s[i] = '0';
continue;
case 'u' :
s[i] = 'u';
continue;
//default :
//printf("default");
}
}
return s;
//printf("%c\n", s[0]);
//n = strlen(s);
//printf("%i\n",n);
//printf("%s\n", s);
//printf("%s\n", s);
//return 1;
}
getting vowels replaced by numbers. Code gives me error as if s is character. What is the fix for this?
As noted in comments, you have not called replace in main. You are simply passing the function itself to printf which decays to a pointer which almost certainly should not be treated as a string by printf. Cue undefined behavior.
You have replace taking a string argument, but then immediately ignoring that passed in char*. The argument serves no purpose and should be eliminated to leave string replace(void).
Assumign we edit replace to not take the extraneous argument, main should look something like:
int main(void)
{
printf("%s\n", replace());
return 0;
}
Now, it would probably be better design for main to prompt for an input string, pass it to replace and have replace modify that string or return a modified version of it which main can then print.

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.

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.

Convert small letter to capital letter

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

Resources