我想通过字符串删除字母g,不使用任何内置函数,只使用一个变量,必须是指针,不允许使用括号。我有代码,但它一直返回一个空字符串而不是新编辑的字符串。
#include <iostream>
using namespace std;
void deleteG(char *str) {
char *temp = str; //make new pointer,pointing at existing, now i have initialized and enough size.
while (*str != '\0') { //while the c-string does not reach null termination
if (*str != 'g' || *str != 'G') { // if the value of the current position is not the character g or G proceed.
*temp = *str;//copy value over
temp++;//increase count
}
str++;//increase count to next char and check again above is if does not equal g or G
}
//this should now copy the new string over to the old string overriding all characters
while (*temp != '\0') {
*str = *temp;
str++;
temp++;
}
}
int main() {
char msg[100] = "I recall the glass gate next to Gus in Lagos, near the gold bridge.";
deleteG(msg);
cout << msg; // prints I recall the lass ate next to us in Laos, near the old bride.
}
答案 0 :(得分:3)
if (*str != 'g' || *str != 'G') {
此条件始终为true,因此始终复制角色。
为什么总是这样,你问?
想一想 - 这个角色是g,或G,或其他东西。
如果是g,则*str != 'g'
为false,*str != 'G'
为真,且false || true
为真,则条件为真。
如果是G,则*str != 'g'
为真,*str != 'G'
为假,且true || false
为真,则条件为真。
如果是其他内容,则*str != 'g'
为真,*str != 'G'
为真,且true || true
为真,则条件为真。
答案 1 :(得分:0)
将其更改为:
<%= form_tag users_path, method: :get do %>
<%= select_tag :sort_type, options_for_select(User.sort_types, :selected => params[:sort_type]), :include_blank => true %>
<% end %>
class User
enum sort_type: ["popularity", "number of photos"]
end
此条件检查字母不 g,无论是否为案件。