#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.
Related
I just started learning programming, and I started with C, and I am just goofing around and trying to make a function that changes a letters in a string from uppercase to all lowercase, and then return it in an array of lowercase letters...
My code doesn't work. And I'm tired of googling. can somebody please help me please?
Here is what I have up until now:
#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
string lowercase(char inlower[]);
int main(void)
{
string word = get_string("Type in a word: ");
char inlower[strlen(word)];
printf("You typed: %s\n", word);
}
string lowercase(string word)
{
for (int i = 0, len = strlen(word); i < len; i++)
{
inlower[i] = tolower(word[i]);
// printf("%c", inlower[i]);
}
return inlower[];
}
You need to work on word and return word, not word[]. inlower is local to main and can't be used in lowercase, unless you pass it along as a parameter along with word.
Also note that you should cast the char in your char[] (string) to unsigned char before using it with tolower. If char is signed and the char[] contains negative values, calling tolower will cause undefined behavior.
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
string lowercase(string word)
{
for (unsigned i = 0, len = strlen(word); i < len; i++)
{
word[i] = tolower((unsigned char) word[i]);
}
return word;
}
int main(void)
{
string word = get_string("Type in a word: ");
printf("You typed: %s\n", lowercase(word));
}
If you do want to put the lowercase word in inlower that you've declared in main, you also need to make it big enough for what you have in word. strlen(word) is one char short since every string must be terminated with a \0 char.
string lowercase(string inlower, string word)
{
unsigned i = 0;
for (unsigned len = strlen(word); i < len; i++)
{
inlower[i] = tolower((unsigned char) word[i]);
}
inlower[i] = '\0';
return inlower;
}
int main(void)
{
string word = get_string("Type in a word: ");
char inlower[strlen(word) + 1]; // correct size
lowercase(inlower, word); // pass `inlower` in to `lowercase` too
printf("You typed: %s\n"
"In lowercase: %s\n", word, inlower);
}
Alternative version without doing strlen inside lowercase too:
string lowercase(string inlower, string word)
{
string dest = inlower;
for(;*word; ++word, ++dest)
{
*dest = tolower((unsigned char) *word);
}
*dest = '\0';
return inlower;
}
I'm learning C recently and I didn't learn about pointers so still not allowed to use it.
Note: i'm not allowed to use it or any function from string.h library.
I wrote a function that removes the "\n" from a string.
when I run my program appears to me:
main.c:20:14: warning: comparison between pointer and integer
main.c:80:39: warning: format not a string literal and no format arguments [-Wformat-security]
This is my function:
#include <stdio.h>
#define STRING_SIZE 100
void replace(char str[]){
int i=0;
while(str[i]!='\0'){
if(str[i]=="\n"){
str[i]='\0';
}
}
}
int my_strlen(char s[]) {
int i = 0;
while (s[i] != '\0') {
i++;
}
return i;
}
int remover(char s1[], char s2[], char s3[]) //removes the sustring s2 from string s1 and saved it to s3
{
int i = 0, j, t = 0, found;
while (s1[i])
{
found = 1;//Initilize found to true
for (j = 0; s2[j] != 0; j++) {
if (s1[i + j] != s2[j])
found = 0;//Set not found
}
if (found == 0) {
s3[t] = s1[i];// if not found add char to s3.
t++;
}
else {
i = i + my_strlen(s2) - 1;//if found skip
}
i++;
}
s3[t] = 0;
if (my_strlen(s1) > my_strlen(s3)) {
return 1;
}
return 0;
}
int main() {
char result_string[STRING_SIZE+1], MainString[STRING_SIZE+1], PatternString[STRING_SIZE+1];
printf("Please enter the main string..\n");
fgets(MainString, STRING_SIZE + 1, stdin);
replace(MainString);
printf("Please enter the pattern string to find..\n");
fgets(PatternString, STRING_SIZE + 1, stdin);
replace(PatternString);
int is_stripped = remover(MainString, PatternString, result_string);
printf("> ");
printf(is_stripped ? result_string : "Cannot find the pattern in the string!");
return 0;
}
what's the problem?
You have a lot of problems:
Your function returns a char, which is a single character.
A C-style string has to have a terminating zero byte. You don't put one on helper. So even if you could return it properly, the code that got it would have no way to know how long the string was.
You allocate helper on the stack in replace, so helper stops existing when you return. So where will the returned string be stored?
Just remove the '\n' from the string, in place, modifying the original string.
For instance:
void remove_newline(char str[])
{
int i;
for(i=0; str[i] != 0; ++i)
{
if (str[i] == '\n')
{
str[i] = 0;
break;
}
}
}
I want to replace every backslash in a string with two backslashes.
I use this code:
#include <stdio.h>
int main()
{
int iD, iM, iY = 0;
char str[255] = "C:\\Users\\Documents";
printf("%s \n", str);
int i = 0;
for(i = 0 ; i < (unsigned)strlen( str ) ; i++)
{
if ( str[i] == '\\')
{
str[i] == "\\\\";
}
}
printf("%s", str);
return 0;
}
Output : C:\Users\Documents
Expected Result : C:\\Users\\Documents
You can't simply replace a single character with two characters! The simplest way to achieve your result is to have a second string, then copy from old to new, one character at a time, adding an extra backslash when that is the character just copied.
Here's a working sample that does this:
#include <stdio.h>
#include <string.h>
int main()
{
char str[255] = "C:\\Users\\Documents";
char newstr[ 2 * sizeof(str) ]; // As suggested by "chux" - ensure buffer is big enough!
printf("%s \n", str);
int i, j;
for (i = j = 0; i < (int)strlen(str); i++, j++) {
newstr[j] = str[i];
if (str[i] == '\\') newstr[++j] = '\\'; // Insert extra backslash
}
newstr[j] = '\0'; // We need to add nul-terminator!
printf("%s", newstr);
return 0;
}
Of course, you could always replace the original string with the new one, once you've done the transformation, with a simple strcpy(str, newstr); line.
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));
I'm trying to brush up on my C/C++ and I seem to have forgotten how to properly manipulate char arrays.
Code:
#include <iostream>
#include <string.h>
void reverse(char* str)
{
int numChar = strlen(str);
char *reversed = (char*)malloc(sizeof(char) * (numChar + 1));
int i = numChar;
int j = 0;
while(i >= 0)
{
reversed[j] = str[i];
j++;
i--;
printf("%c", reversed[j]);
}
printf("%s", reversed);
}
int main()
{
char* str;
strcpy(str, "apple\0");
reverse(str);
return 0;
}
I'm very certain I'm not doing what I intend to with reversed[j] = str[i] as reversed comes out empty. What's the correct way to go about this?
From first glance, In main(), memory has to be allocated to character pointer str before referencing it in strcpy
int main()
{
char* str = malloc(6) or use char str[6];
// or char *str = "apple"; is sufficient, strcpy is not required in this case
strcpy(str, "apple\0");
reverse(str);
return 0;
}
Another one :
In reverse() function, you will have to increment j after printing
while(i >= 0)
{
reversed[j] = str[i];
printf("%c", reversed[j]);
j++; //Moved here
i--;
}
reversed[j] = '\0' //Null termination
printf("\n %s", reversed);
or only the below two statements would be sufficient enough to post increment j and decrement i
reversed[j] = str[i--];
printf("%c", reversed[j++]);
Since you start with i being the strlen of the input string (e.g. HAHA -> 4), you start copying at haha[4], which is the null byte at the end of the string. IOW you just null terminated your output right at the start. Try setting i to numChar - 1.
(After fixing the problem Santosh A mentioned)
And then, make sure you null terminate the result!