C :: Aufgabe #168
3 Lösungen
Groß- und Kleinuchstaben
Anfänger - C
von MeLThRoX
- 22.08.2017 um 19:03 Uhr
Erstelle ein Programm, welches in einem String die Kleinbuchstaben in Großbuchstaben umwandelt und andersherum. Zahlen und Zusatzzeichen sollen nicht beachtet werden
Lösungen:
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <wctype.h>
#include <stdio.h>
#include <locale.h>
#define BUFLEN 256
/* convert multi byte string from upper to lower an vice versa */
char* ul2lu(char *out, const char *in) {
mbstate_t ps, pst;
const char *mbin;
char *mbout;
wchar_t wc, wct;
size_t wcl, wctl;
memset(&ps, 0, sizeof(mbstate_t));
memset(&pst, 0, sizeof(mbstate_t));
mbin = in;
mbout = out;
while ((wcl = mbrtowc(&wc, mbin, MB_CUR_MAX, &ps)) > 0) {
wct = (iswlower(wc))?(wchar_t)towupper(wc):(wchar_t)towlower(wc);
wctl = wcrtomb(mbout, wct, &pst);
mbout += wctl;
mbin += wcl;
}
wcrtomb(mbout, L'\0', &pst); /* terminate mb string */
return out;
}
int main(int argc, char **argv) {
char outbuf[BUFLEN];
printf("locale = %s\n", setlocale(LC_ALL, ""));
if (argc > 1) {
printf("your string: %s\n", argv[1]);
printf(" converted: %s\n", ul2lu(outbuf, argv[1]));
}
return 0;
}
int main (void){
printf("geben Sie einen String nur mit Kleinbuchstaben ein ein\n");
char string[100];
scanf("%s" , &string);
int i=0;
while(string[i] != '\0')
{
string[i]-=32;
i++;
}
printf("%s", string\n);
printf("geben Sie einen String nur mit Großbuchstaben ein ein\n");
scanf("%s" , &string);
i=0;
while(string[i] != '\0')
{
string[i]+=32;
i++;
}
printf("%s", string);
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char string[100];
printf("Enter string: ");
gets(string);
printf("You entered: %s", string);
unsigned long length = strlen(string);
for(int i = 0; i < length; i++){
//lower case letters to upper case letters
if((int)string[i]>=97 && string[i]<=122){
string[i] = string[i] - 32;
}
//upper to lower
else if((int)string[i]>=65 && string[i]<=90){
string[i] = string[i] + 32;
}
}
printf("\nConverted string: %s\n",string);
return 0;
}